Spring之AOP日志管理配置示例(注解方式)
1 package com.test; 2 3 import java.util.List; 4 5 public interface TestService { 6 public void test(String testStr,int age,List list); 7 }
1 package com.test; 2 3 import java.util.List; 4 5 public class TestServiceImpl implements TestService{ 6 public void test(String testStr, int age, List list) { 7 // TODO Auto-generated method stub 8 System.out.println("test TestServiceImpl"); 9 } 10 }
1 package com.test; 2 3 import org.aspectj.lang.JoinPoint; 4 import org.aspectj.lang.annotation.After; 5 import org.aspectj.lang.annotation.AfterThrowing; 6 import org.aspectj.lang.annotation.Aspect; 7 import org.aspectj.lang.annotation.Before; 8 import org.aspectj.lang.annotation.Pointcut; 9 import org.springframework.stereotype.Component; 10 11 12 @Aspect 13 @Component 14 public class LogAspect { 15 16 /** 17 * execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?) 18 1.modifiers-pattern:方法的操作权限 19 2.ret-type-pattern:返回值 20 3.declaring-type-pattern:方法所在的包 21 4.name-pattern:方法名 22 5.parm-pattern:参数名 23 6.throws-pattern:异常 24 说明:除ret-type-pattern和name-pattern之外,其他都是可选的。上例中,execution(* com.spring.service.*.*(..))表示com.spring.service包下,返回值为任意类型;方法名任意;参数不作限制的所有方法。 25 */ 26 27 private final String cutValue = "execution(public * com.test.*.*.*(..))"; 28 29 /** 30 * 配置前置通知 31 * @param jp 32 * @throws Exception 33 */ 34 @Before(cutValue) 35 public void beforeShow(JoinPoint jp) { 36 // HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.getRequestAttributes())).getRequest(); //((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); 37 //// 请求的IP 38 // String ip = request.getRemoteAddr(); 39 // System.out.println("请求ip地址:"+ip); 40 // String className = jp.getThis().toString(); 41 // System. out.println("====位于:" +className); 42 String sName = jp.getTarget().getClass().getName(); 43 System.out.println("【类名】"+sName); 44 String methodName = jp.getSignature().getName(); //获得方法名 45 System. out.println("【调用方法】" +methodName); 46 Object[] args = jp.getArgs(); //获得参数列表 47 for (Object object : args) { 48 System. out.println("【请求参数 】:"+object); 49 } 50 System. out.println("【" +jp.getSignature().getName()+"】方法调用开始!"); 51 52 } 53 54 /** 55 * 配置后置通知 56 * @param jp 57 */ 58 @After(cutValue) 59 public void afterShow(JoinPoint jp) { 60 System. out.println("【" +jp.getSignature().getName()+"】方法调用结束!"); 61 } 62 63 /** 64 * Service 切点 65 * 配置切入点,该方法无方法体,主要为方便同类中其他方法使用此处配置的切入点 66 */ 67 @Pointcut(cutValue) 68 public void aspectExecption(){ } 69 70 /** 71 * 配置抛出异常后通知,使用在方法aspectExecption()上注册的切入点 72 * @param jp 73 * @param ex 74 */ 75 @AfterThrowing(pointcut = "aspectExecption()", throwing = "ex") 76 public void doAfterThrowing(JoinPoint jp, Throwable ex) { 77 System.out.println("【"+jp.getSignature().getName()+"】异常信息:"+ex); 78 } 79 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 4 xsi:schemaLocation=" 5 http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 7 http://www.springframework.org/schema/aop 8 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 11 <aop:aspectj-autoproxy/> 12 <!-- 注入日志类 LogAspect--> 13 <bean id="logAspect" class="com.test.LogAspect"/> 14 <!-- 注入业务类 --> 15 <bean name="test" class="com.test.TestServiceImpl"></bean> 16 <!-- AOP配置 --> 17 <aop:config> 18 <aop:aspect id="simpleApect" ref="logAspect"> 19 <!-- 配置切入点 --> 20 <aop:pointcut expression="execution(* com.test..*(..))" id="simplePointCut"/> 21 <!-- 配置通知 --> 22 <aop:before method="beforeShow" pointcut-ref="simplePointCut"/> 23 <aop:after method="afterShow" pointcut-ref="simplePointCut"/> 24 <!-- <aop:after-returning method="afterReturn" pointcut-ref="simplePointCut"/> --> 25 <aop:after-throwing method="doAfterThrowing" pointcut-ref="simplePointCut" throwing="ex"/> 26 </aop:aspect> 27 </aop:config> 28 </beans>
1 package com.test; 2 3 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.springframework.context.support.ClassPathXmlApplicationContext; 8 9 public class TestMain{ 10 public static void main(String[] args) { 11 //获取BeanFactory 12 ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml"); 13 TestService test = (TestService)factory.getBean("test"); 14 List l = new ArrayList(); 15 l.add("param1"); 16 l.add("param2"); 17 l.add("param3"); 18 //调用测试方法 19 test.test("ok",1,l); 20 } 21 }
日志管理有两种方式:一种是注解方式,另外一种是xml配置方式(后续研究...)。主要用来与项目业务逻辑层进行分割,就是所谓的解耦和,便于日后管理及维护。
测试时,需要导入Spring的包有:Spring基础包、Spring AOP相关包。

浙公网安备 33010602011771号