spring:利用Spring AOP 使日志输入与方法分离

对方法进行日志输出是一种很常见的功能。传统的做法是把输出语句写在方法体的内部,在调用该方法时,用输入语句输出信息来记录方法的执行!

1.先写一个普通类:

package com.importnew;

public class Common {

    public void execute(String username,String password){
         System.out.println("------------------执行 execute()方法----------------");
       }
}

 

2.写一个切面类,用于合法性校验和日志添加:

package com.importnew;
import org.aspectj.lang.JoinPoint;
public class Check { public void checkValidity(){ System.out.println("------------------验证合法性----------------"); } public void addLog(JoinPoint j){ System.out.println("------------------添加日志----------------"); Object obj[] = j.getArgs(); for(Object o :obj){ System.out.println(o); } System.out.println("========checkSecurity=="+j.getSignature().getName());//这个是获得方法名 } }

 

3.配置AOP,使用XML方式:(注意红色标志的内容)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
             
      <bean id="common" class="com.importnew.Common"/>      
      <bean id="check" class="com.importnew.Check"/>
       
      <aop:config>
        <aop:aspect id="myAop" ref="check">
          <aop:pointcut id="target" expression="execution(* com.importnew.Common.execute(..))"/>
          <aop:before method="checkValidity" pointcut-ref="target"/>
          <aop:after method="addLog" pointcut-ref="target"/>
        </aop:aspect>
      </aop:config>     
</beans>

 


注意:aop pointcut表达式(*)
execution(方法修饰符+返回值 完整类名+方法名(方法参数))
例如:
A、execution(public void *(..)):所有返回值是public void的方法都会被拦截到
B、execution(public void day6.com.beans.PersonService.*(..)):表示day6.com.beans.PersonService下所有返回值是public void的方法都会被拦截到
C、 execution(public void day6.com.beans.PersonService.save(java.lang.String...)):表示 day6.com.beans.PersonService类中的第一个形参类型是String的save方法会被拦截到
D、execution(public void save(..)):表示所有类中的save方法会被拦截到
E、execution(public void day6.com.service..*(..)):表示day6.com.service包下的类以及子包的类所有public void方法都会被拦截到
F、execution(public !void day6.com.service..*(..)):表示day6.com.service包下的类以及子包的类所有public 不是void返回类型的方法都会被拦截到


4.最后写一个测试:

package test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.importnew.Common;

public class Client {

    public static void main(String[] args) {
        BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
         Common c=(Common) factory.getBean("common");
         c.execute("fuckyou","fuckme");
    }
}

 

注意:

需要添加三个包:spring-aop.jar , aspectjrt.jar ,aspectjweaver.jar,否则会报错。

输出结果:

------------------验证合法性----------------
------------------执行 execute()方法----------------
------------------添加日志----------------
fuckyou
fuckme
========checkSecurity==execute

 

 

////end

posted @ 2016-10-20 11:23  Hosens  阅读(787)  评论(0编辑  收藏  举报