分享

Spring AOP进行日志记录,管理

 小丑g22xft6chp 2016-06-20

Java开发中日志的管理有很多种。我一般会使用过滤器,或者是spring的拦截器进行日志的处理。如果是用过滤器比较简单,只要对所有的.do提交进行拦截,然后获取action的提交路径就可以获取对每个方法的调用。然后进行日志记录。使用过滤器的好处是可以自己选择性的对某一些方法进行过滤,记录日志。但是实现起来有点麻烦。

             另外一种就是使用Spring的AOP了。这种方式实现起来非常简单,只要配置一下配置文件就可以了。可是这种方式会拦截下所有的对action的每个操作。使得效率比较低。不过想做详细日志这个方法还是非常好的。下面我就介绍一下使用Spring AOP进行日志记录的方式。

        第一种。Spring AOP对普通类的拦截操作

             首先我们要写一个普通类,此类作为日志记录类。 比如

 

  1. <span style="font-size:12px;">            package chen.hui.log  
  2.              public classs MyLog{  
  3.                        //在类里面写方法,方法名诗可以任意的。此处我用标准的before和after来表示  
  4.                       public void before(){  
  5.                                 System.out.println("被拦截方法调用之前调用此方法,输出此语句");  
  6.                       }  
  7.                       public void after(){  
  8.                                   System.out.println("被拦截方法调用之后调用此方法,输出此语句");  
  9.                       }  
  10.             }  
  11.              其次我们在写一个类作为被拦截类(Spring的AOP就是拦截这个类里面的方法)  
  12.              package chen.hui.log  
  13.              public class Test{//此类中方法可以写任意多个。我只写一个  
  14.                           public void test(){  
  15.                                 Sytem.out.println("测试类的test方法被调用");  
  16.                           }  
  17.              }</span>  

  最后进行配置文件的编写。在Spring的配置文件中我们需要进行几句话的配置

  1. <span style="font-size:12px;">          <bean id="testLog" class="chen.hui.log.MyLog"></bean> <!--将日志类注入到bean中。-->  
  2.        
  3.                  <aop:config>  
  4.                              <aop:aspect id="b" ref="testLog"><!--调用日志类-->  
  5.                             <aop:pointcut id="log" expression="execution(* chen.hui.log.*.*(..))"/><!--配置在log包下所有的类在调用之前都会被拦截-->  
  6.                             <aop:before pointcut-ref="log" method="before"/><!--在log包下面所有的类的所有方法被调用之前都调用MyLog中的before方法-->  
  7.                             <aop:after pointcut-ref="log" method="after"/>><!--在log包下面所有的类的所有方法被调用之前都调用MyLog中的after方法-->  
  8.   
  9.                             </aop:aspect>  
  10.      
  11.                  </aop:config></span>  


 

到此处整个程序完成,在MyLog类里面的before和after方法添加日志逻辑代码就可以完成日志的管理。以上是对普通类的管理,如果只想拦截某一个类。只要把倒数第二个 *  改成类名就可以了。

 

          第二:使用Spring AOP对action做日志管理

          如果是想拦截action对action做日志管理,基本和上面差不多,但是要注意。以下几点

首先还是要写一个普通类,不过此类中的方法需要传入参数。 比如

  1. <span style="font-size:12px;">            package chen.hui.log  
  2.             import org.aspectj.lang.JoinPoint;  
  3.              public classs MyLog{  
  4.                        //在类里面写方法,方法名诗可以任意的。此处我用标准的before和after来表示  
  5.                         //此处的JoinPoint类可以获取,action所有的相关配置信息和request等内置对象。  
  6.                       public void before(JoinPoint joinpoint){  
  7.                                 joinpoint.getArgs();//此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象  
  8.                                 System.out.println("被拦截方法调用之前调用此方法,输出此语句");  
  9.                       }  
  10.                       public void after(JoinPoint joinpoint){  
  11.                                   System.out.println("被拦截方法调用之后调用此方法,输出此语句");  
  12.                       }  
  13.             }</span>  


 其次我们在写一个action类作为被拦截类(Spring的AOP就是拦截这个类里面的方法)

  1. <span style="font-size:12px;">             package chen.hui.log  
  2.              public class LoginAction{//此类中方法可以写任意多个。我只写一个  
  3.                           public void test(){  
  4.                                 Sytem.out.println("测试类的test方法被调用");  
  5.                           }  
  6.              }</span>  


  最后进行配置文件的编写。在Spring的配置文件中我们需要进行几句话的配置

  1. <span style="font-size:12px;">             <bean id="testLog" class="chen.hui.log.MyLog"></bean> <!--将日志类注入到bean中。-->  
  2.        
  3.                  <aop:config>  
  4.                              <aop:aspect id="b" ref="testLog"><!--调用日志类-->  
  5.                             <aop:pointcut id="log" expression="execution(* chen.hui.log.*.*(..))"/><!--配置在log包下所有的类在调用之前都会被拦截-->  
  6.                             <aop:before pointcut-ref="log" method="before"/><!--在log包下面所有的类的所有方法被调用之前都调用MyLog中的before方法-->  
  7.                             <aop:after pointcut-ref="log" method="after"/>><!--在log包下面所有的类的所有方法被调用之前都调用MyLog中的after方法-->  
  8.   
  9.                             </aop:aspect>  
  10.      
  11.                  </aop:config></span>  


 

  除了参数外其他地方基本和普通类相似。

               需要注意的是:普通类可以监控单一的类,而action在配置文件中只能到包名而不能到action的类名。不然会报错。就是说如果要记录日志就要记录所有的action而不能记录其中一个,这是我试了好久得出的结果。



.实现登陆和日志管理(使用Spring AOP

1)LoginService   LogService   TestMain

2)用Spring 管理  LoginService 和 LogService 的对象

3)确定哪些连接点是切入点,在配置文件中

4)将LogService封装为通知

5)将通知植入到切入点

6)客户端调用目标



  1. <aop:config>
  2.     <aop:pointcut expression="execution(* cn.com.spring.service.impl.*.*(..))" id="myPointcut"/>
  3.     <!--将哪个-->
  4.     <aop:aspect id="dd" ref="logService">
  5.       <aop:before method="log" pointcut-ref="myPointcut"/>
  6.     </aop:aspect>
  7. </aop:config>

execution(* * cn.com.spring.service.impl.*.*(..))

1)* 所有的修饰符

2)* 所有的返回类型

3)* 所有的类名

4)* 所有的方法名

5)* ..所有的参数名


1.ILoginService.java


  1. package cn.com.spring.service;

  2. public interface ILoginService {
  3.     public boolean login(String userName, String password);
  4. }

2.LoginServiceImpl.java

 


 

 

  1. package cn.com.spring.service.impl;

  2. import cn.com.spring.service.ILoginService;

  3. public class LoginServiceImpl implements ILoginService {

  4.     public boolean login(String userName, String password) {
  5.         System.out.println("login:" + userName + "," + password);
  6.         return true;
  7.     }

  8. }

 

 

3.ILogService.java

 


 

 

  1. package cn.com.spring.service;

  2. import org.aspectj.lang.JoinPoint;

  3. public interface ILogService {
  4.     //无参的日志方法
  5.     public void log();
  6.     //有参的日志方法
  7.     public void logArg(JoinPoint point);
  8.     //有参有返回值的方法
  9.     public void logArgAndReturn(JoinPoint point,Object returnObj);
  10. }


4.LogServiceImpl.java

 


 

 

  1. package cn.com.spring.service.impl;

  2. import org.aspectj.lang.JoinPoint;

  3. import cn.com.spring.service.ILogService;

  4. public class LogServiceImpl implements ILogService {

  5.     @Override
  6.     public void log() {
  7.         System.out.println("*************Log*******************");
  8.     }
  9.     
  10.     //有参无返回值的方法
  11.     public void logArg(JoinPoint point) {
  12.         //此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象
  13.         Object[] args = point.getArgs();
  14.         System.out.println("目标参数列表:");
  15.         if (args != null) {
  16.             for (Object obj : args) {
  17.                 System.out.println(obj + ",");
  18.             }
  19.             System.out.println();
  20.         }
  21.     }

  22.     //有参并有返回值的方法
  23.     public void logArgAndReturn(JoinPoint point, Object returnObj) {
  24.         //此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象
  25.         Object[] args = point.getArgs();
  26.         System.out.println("目标参数列表:");
  27.         if (args != null) {
  28.             for (Object obj : args) {
  29.                 System.out.println(obj + ",");
  30.             }
  31.             System.out.println();
  32.             System.out.println("执行结果是:" + returnObj);
  33.         }
  34.     }
  35. }

5.applicationContext.java

 


 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www./schema/beans"
  3.     xmlns:aop="http://www./schema/aop" xmlns:xsi="http://www./2001/XMLSchema-instance"
  4.     xmlns:p="http://www./schema/p"
  5.     xsi:schemaLocation="http://www./schema/beans 
  6.     http://www./schema/beans/spring-beans-2.5.xsd
  7.     http://www./schema/aop 
  8.     http://www./schema/aop/spring-aop-2.5.xsd">

  9.     <bean id="logService" class="cn.com.spring.service.impl.LogServiceImpl"></bean>
  10.     <bean id="loginService" class="cn.com.spring.service.impl.LoginServiceImpl"></bean>

  11.     <aop:config>
  12.         <!-- 切入点 -->
  13.         <aop:pointcut
  14.             expression="execution(* cn.com.spring.service.impl.LoginServiceImpl.*(..))"
  15.             id="myPointcut" />
  16.         <!-- 切面: 将哪个对象中的哪个方法,织入到哪个切入点 -->
  17.         <aop:aspect id="dd" ref="logService">
  18.             <!-- 前置通知
  19.             <aop:before method="log" pointcut-ref="myPointcut" />
  20.             <aop:after method="logArg" pointcut-ref="myPointcut"> 
  21.     -->
  22.             <aop:after-returning method="logArgAndReturn" returning="returnObj" pointcut-ref="myPointcut"/>
  23.         </aop:aspect>
  24.     </aop:config>
  25. </beans>

6.TestMain.java

 


 

  1. public class TestMain {
  2. public static void testSpringAOP(){
  3.         ApplicationContext ctx = new ClassPathXmlApplicationContext("app*.xml");
  4.         
  5.         ILoginService loginService = (ILoginService)ctx.getBean("loginService");
  6.         loginService.login("zhangsan", "12344");
  7. }
  8. public static void main(String[] args) {
  9. testSpringAOP();
  10. }
  11. }

 

7.输出结果:

 


  1. login:zhangsan,12344
  2. 目标参数列表:
  3. zhangsan,
  4. 12344,

  5. 执行结果是:true

解析:1.先调用了login()方法System.out.println("login:" + userName + "," + password);

 

     2.再调用了logArgAndReturn()方法输出了日志,并且返回了login()方法是否成功

 


 

  1. System.out.println("目标参数列表:");
  2.         if (args != null) {
  3.             for (Object obj : args) {
  4.                 System.out.println(obj + ",");
  5.             }
  6.             System.out.println();
  7.             System.out.println("执行结果是:" + returnObj);
  8.         }

原文地址:http://blog.csdn.net/jazywoo123/article/details/8247005#

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多