springAOP的两种配置方法



在使用的时候,遇到了部分的异常,我用的是最新的spring版本,Spring-4.2.5版本的,首先确保你的配置文件中引入了下面红色部分。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.       <span style="color:#ff0000;"> xmlns:aop="http://www.springframework.org/schema/aop"</span>  
  4.        xsi:schemaLocation="  
  5. http://www.springframework.org/schema/beans   
  6. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  7. <span style="color:#ff0000;">http://www.springframework.org/schema/aop  
  8. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"</span>>  

然后需要导入的包有几个:

1. aspectjrt.jar

2. aspectjweaver.jar

3. aopalliance-1.0.jar


我在使用的时候由于没有使用第三个jar包导致了异常信息如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. 警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor  
  2. Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor  
  3.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105)  
  4.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1050)  
  5.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)  
  6.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)  
  7.     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)  
  8.     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)  
  9.     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)  
  10.     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)  
  11.     at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:228)  
  12.     at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:687)  
  13.     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:523)  
  14.     at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)  
  15.     at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)  
  16.     at com.siti.spring20160315aop.MainTest.main(MainTest.java:9)  
  17. Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor  
  18.     at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)  
  19.     at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)  
  20.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1098)  
  21.     ... 13 more  


下面开始使用AspectJ进行切面配置



AspectJ注解方式进行切面配置


定义四个接口,下面让WangYang类实现这个Person的接口,并实现其中的方法。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.siti.spring20160315aop;  
  2.   
  3. public interface Person {  
  4.   
  5.     void say();  
  6.     void sayName(String name);  
  7.     String saySth(String name);  
  8.     void sayEx();  
  9. }  


其中saySth这个方法设置了返回值,这个地方会被AfterReturning织入,然后sayEx方法为了测试故意利用空指针异常,测试AfterThrowing会不会被织入。


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.siti.spring20160315aop;  
  2.   
  3. public class WangYang implements Person{  
  4.   
  5.     @Override  
  6.     public void say() {  
  7.         System.out.println("wy!");  
  8.     }  
  9.   
  10.     @Override  
  11.     public void sayName(String name) {  
  12.         System.out.println("name-->" + name);  
  13.     }  
  14.   
  15.     @Override  
  16.     public String saySth(String name) {  
  17.         System.out.println("name-->" + name);  
  18.         return name;  
  19.     }  
  20.   
  21.     @Override  
  22.     public void sayEx() {  
  23.         Object obj = null;  
  24.         obj.equals("1");  
  25.     }  
  26. }  


面向切面编程AspectJ的实现,主要方式如下,其中,after通知主要用于释放资源,afterReturning通知可以对返回值进行更改,afterThrowing通知可以进行异常捕获生成异常记录,before这个通知可以进行参数的校验之类的。
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.siti.spring20160315aop;  
  2.   
  3. import java.util.Arrays;  
  4.   
  5. import org.aspectj.lang.JoinPoint;  
  6. import org.aspectj.lang.ProceedingJoinPoint;  
  7. import org.aspectj.lang.annotation.After;  
  8. import org.aspectj.lang.annotation.AfterReturning;  
  9. import org.aspectj.lang.annotation.AfterThrowing;  
  10. import org.aspectj.lang.annotation.Around;  
  11. import org.aspectj.lang.annotation.Aspect;  
  12. import org.aspectj.lang.annotation.Before;  
  13. import org.aspectj.lang.annotation.Pointcut;  
  14.   
  15. @Aspect  
  16. public class MyInterceptor {  
  17.   
  18.     @Pointcut("execution(* com.siti.spring20160315aop.WangYang.*(..))")  
  19.     public void anyMethod(){}  
  20.       
  21.     @Before("anyMethod()")  
  22.     public void checkMessage(JoinPoint joinPoint){  
  23.         System.out.println("@Before-check!!!");  
  24.         System.out.println("@Before-目标对象为:" + joinPoint.getTarget());  
  25.         System.out.println("@Before-目标方法:" + joinPoint.getSignature().getName());  
  26.         System.out.println("@Before-目标方法的参数:" + Arrays.toString(joinPoint.getArgs()));  
  27.     }  
  28.       
  29.     @AfterReturning(pointcut = "anyMethod()", returning = "obj")  
  30.     public void checkReturn(JoinPoint joinPoint, Object obj){  
  31.         System.out.println("@AfterReturning-目标方法返回值:" + obj);  
  32.         System.out.println("@AfterReturning-生成日志");  
  33.           
  34.         System.out.println("@AfterReturning-目标对象为:" + joinPoint.getTarget());  
  35.         System.out.println("@AfterReturning-目标方法:" + joinPoint.getSignature().getName());  
  36.         System.out.println("@AfterReturning-目标方法的参数:" + Arrays.toString(joinPoint.getArgs()));  
  37.     }  
  38.       
  39.     @After("anyMethod()")  
  40.     public void checkAfter(JoinPoint joinPoint){  
  41.         System.out.println("@After-释放资源!");  
  42.           
  43.         System.out.println("@After-目标对象为:" + joinPoint.getTarget());  
  44.         System.out.println("@After-目标方法:" + joinPoint.getSignature().getName());  
  45.         System.out.println("@After-目标方法的参数:" + Arrays.toString(joinPoint.getArgs()));  
  46.     }  
  47.       
  48.     @Around("anyMethod()")  
  49.     public Object checkAround(ProceedingJoinPoint joinPoint) throws Throwable{  
  50.         System.out.println("@Around!");  
  51.           
  52.         Object[] args = joinPoint.getArgs();  
  53.         if(args != null && args.length > 0 && args[0].getClass() == String.class){  
  54.             args[0] = "@Around" + args[0];  
  55.         }  
  56.         Object result = joinPoint.proceed(args);  
  57.         if(result != null && result.getClass() == String.class){  
  58.             result = result + "--wy";  
  59.         }  
  60.         return result;  
  61.     }  
  62.       
  63.     @AfterThrowing(throwing = "ex", pointcut = "anyMethod()")  
  64.     public void checkAfterThrowing(Throwable ex){  
  65.         System.out.println("@AfterThrowing-异常抛出!");  
  66.         System.out.println("@AfterThrowing-异常日志!");  
  67.           
  68.     }  
  69.       
  70.       
  71. }  


配置文件需要将我们自己写的这个切面的类,在配置文件中进行“注册”。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:aop="http://www.springframework.org/schema/aop"  
  5.        xsi:schemaLocation="  
  6. http://www.springframework.org/schema/beans   
  7. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  8. http://www.springframework.org/schema/aop  
  9. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">  
  10.     <aop:aspectj-autoproxy/>  
  11.       
  12.     <bean id = "inter" class = "com.siti.spring20160315aop.MyInterceptor"></bean>  
  13.     <bean id = "wy" class = "com.siti.spring20160315aop.WangYang"></bean>  
  14. </beans>  
测试:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.siti.spring20160315aop;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class MainTest {  
  7.   
  8.     public static void main(String[] args) {  
  9.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext20160315.xml");  
  10.         Person wy = context.getBean("wy", Person.class);  
  11.         wy.say();  
  12.         wy.sayName("wangyang-");  
  13.         String str = wy.saySth("hello--");  
  14.         System.out.println("str的值:" + str);  
  15.         // wy.sayEx();// 抛出异常的测试  
  16.     }  
  17. }  




XML配置文件的方式


基于配置文件配置的方式和前面的注解的方式基本上类似的,这里只是将,改动的地方列出来,不做介绍了。

配置文件如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:aop="http://www.springframework.org/schema/aop"  
  5.        xsi:schemaLocation="  
  6. http://www.springframework.org/schema/beans   
  7. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  8. http://www.springframework.org/schema/aop  
  9. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">  
  10.       
  11.     <aop:config>  
  12.         <!-- 定义公共的切入点表达式 -->  
  13.         <aop:pointcut expression="execution(* com.siti.spring20160315aopwithxml.WangYang.*(..))" id="pointCutCommon"/>  
  14.         <!-- 定义切面,命名为 adviceRespect 优先级是2 ref:表示对谁进行增强处理-->  
  15.         <aop:aspect id = "adviceRespect" ref = "inter" order="2">  
  16.             <aop:before pointcut-ref="pointCutCommon" method="checkMessage"/>  
  17.             <aop:after-returning pointcut-ref="pointCutCommon" method="checkReturn" returning="obj"/>  
  18.             <aop:around pointcut-ref="pointCutCommon" method="checkAround"/>  
  19.             <aop:after pointcut-ref="pointCutCommon" method="checkAfter"/>  
  20.         </aop:aspect>  
  21.         <aop:aspect id = "firstExecute" ref = "inter" order = "1">  
  22.             <aop:after-throwing pointcut-ref="pointCutCommon" method="checkAfterThrowing" throwing="ex"/>  
  23.         </aop:aspect>   
  24.     </aop:config>  
  25.     <bean id = "inter" class = "com.siti.spring20160315aopwithxml.MyInterceptor"></bean>  
  26.     <bean id = "wy" class = "com.siti.spring20160315aopwithxml.WangYang"></bean>  
  27. </beans>  

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.siti.spring20160315aopwithxml;  
  2.   
  3. import java.util.Arrays;  
  4.   
  5. import org.aspectj.lang.JoinPoint;  
  6. import org.aspectj.lang.ProceedingJoinPoint;  
  7.   
  8. public class MyInterceptor {  
  9.   
  10.     public void anyMethod(){}  
  11.       
  12.     public void checkMessage(JoinPoint joinPoint){  
  13.         System.out.println("@Before-check!!!");  
  14.         System.out.println("@Before-目标对象为:" + joinPoint.getTarget());  
  15.         System.out.println("@Before-目标方法:" + joinPoint.getSignature().getName());  
  16.         System.out.println("@Before-目标方法的参数:" + Arrays.toString(joinPoint.getArgs()));  
  17.     }  
  18.       
  19.     public void checkReturn(JoinPoint joinPoint, Object obj){  
  20.         System.out.println("@AfterReturning-目标方法返回值:" + obj);  
  21.         System.out.println("@AfterReturning-生成日志");  
  22.           
  23.         System.out.println("@AfterReturning-目标对象为:" + joinPoint.getTarget());  
  24.         System.out.println("@AfterReturning-目标方法:" + joinPoint.getSignature().getName());  
  25.         System.out.println("@AfterReturning-目标方法的参数:" + Arrays.toString(joinPoint.getArgs()));  
  26.     }  
  27.       
  28.     public void checkAfter(JoinPoint joinPoint){  
  29.         System.out.println("@After-释放资源!");  
  30.           
  31.         System.out.println("@After-目标对象为:" + joinPoint.getTarget());  
  32.         System.out.println("@After-目标方法:" + joinPoint.getSignature().getName());  
  33.         System.out.println("@After-目标方法的参数:" + Arrays.toString(joinPoint.getArgs()));  
  34.     }  
  35.       
  36.     public Object checkAround(ProceedingJoinPoint joinPoint) throws Throwable{  
  37.         System.out.println("@Around!");  
  38.           
  39.         Object[] args = joinPoint.getArgs();  
  40.         if(args != null && args.length > 0 && args[0].getClass() == String.class){  
  41.             args[0] = "@Around" + args[0];  
  42.         }  
  43.         Object result = joinPoint.proceed(args);  
  44.         if(result != null && result.getClass() == String.class){  
  45.             result = result + "--wy";  
  46.         }  
  47.         return result;  
  48.     }  
  49.       
  50.     public void checkAfterThrowing(Throwable ex){  
  51.         System.out.println("@AfterThrowing-异常抛出!");  
  52.         System.out.println("@AfterThrowing-异常日志!");  
  53.           
  54.     }  
  55.       
  56. }  

在使用的时候,遇到了部分的异常,我用的是最新的spring版本,Spring-4.2.5版本的,首先确保你的配置文件中引入了下面红色部分。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.       <span style="color:#ff0000;"> xmlns:aop="http://www.springframework.org/schema/aop"</span>  
  4.        xsi:schemaLocation="  
  5. http://www.springframework.org/schema/beans   
  6. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  7. <span style="color:#ff0000;">http://www.springframework.org/schema/aop  
  8. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"</span>>  

然后需要导入的包有几个:

1. aspectjrt.jar

2. aspectjweaver.jar

3. aopalliance-1.0.jar


我在使用的时候由于没有使用第三个jar包导致了异常信息如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. 警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor  
  2. Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor  
  3.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105)  
  4.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1050)  
  5.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)  
  6.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)  
  7.     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)  
  8.     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)  
  9.     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)  
  10.     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)  
  11.     at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:228)  
  12.     at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:687)  
  13.     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:523)  
  14.     at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)  
  15.     at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)  
  16.     at com.siti.spring20160315aop.MainTest.main(MainTest.java:9)  
  17. Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor  
  18.     at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)  
  19.     at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)  
  20.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1098)  
  21.     ... 13 more  


下面开始使用AspectJ进行切面配置



AspectJ注解方式进行切面配置


定义四个接口,下面让WangYang类实现这个Person的接口,并实现其中的方法。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.siti.spring20160315aop;  
  2.   
  3. public interface Person {  
  4.   
  5.     void say();  
  6.     void sayName(String name);  
  7.     String saySth(String name);  
  8.     void sayEx();  
  9. }  


其中saySth这个方法设置了返回值,这个地方会被AfterReturning织入,然后sayEx方法为了测试故意利用空指针异常,测试AfterThrowing会不会被织入。


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.siti.spring20160315aop;  
  2.   
  3. public class WangYang implements Person{  
  4.   
  5.     @Override  
  6.     public void say() {  
  7.         System.out.println("wy!");  
  8.     }  
  9.   
  10.     @Override  
  11.     public void sayName(String name) {  
  12.         System.out.println("name-->" + name);  
  13.     }  
  14.   
  15.     @Override  
  16.     public String saySth(String name) {  
  17.         System.out.println("name-->" + name);  
  18.         return name;  
  19.     }  
  20.   
  21.     @Override  
  22.     public void sayEx() {  
  23.         Object obj = null;  
  24.         obj.equals("1");  
  25.     }  
  26. }  


面向切面编程AspectJ的实现,主要方式如下,其中,after通知主要用于释放资源,afterReturning通知可以对返回值进行更改,afterThrowing通知可以进行异常捕获生成异常记录,before这个通知可以进行参数的校验之类的。
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.siti.spring20160315aop;  
  2.   
  3. import java.util.Arrays;  
  4.   
  5. import org.aspectj.lang.JoinPoint;  
  6. import org.aspectj.lang.ProceedingJoinPoint;  
  7. import org.aspectj.lang.annotation.After;  
  8. import org.aspectj.lang.annotation.AfterReturning;  
  9. import org.aspectj.lang.annotation.AfterThrowing;  
  10. import org.aspectj.lang.annotation.Around;  
  11. import org.aspectj.lang.annotation.Aspect;  
  12. import org.aspectj.lang.annotation.Before;  
  13. import org.aspectj.lang.annotation.Pointcut;  
  14.   
  15. @Aspect  
  16. public class MyInterceptor {  
  17.   
  18.     @Pointcut("execution(* com.siti.spring20160315aop.WangYang.*(..))")  
  19.     public void anyMethod(){}  
  20.       
  21.     @Before("anyMethod()")  
  22.     public void checkMessage(JoinPoint joinPoint){  
  23.         System.out.println("@Before-check!!!");  
  24.         System.out.println("@Before-目标对象为:" + joinPoint.getTarget());  
  25.         System.out.println("@Before-目标方法:" + joinPoint.getSignature().getName());  
  26.         System.out.println("@Before-目标方法的参数:" + Arrays.toString(joinPoint.getArgs()));  
  27.     }  
  28.       
  29.     @AfterReturning(pointcut = "anyMethod()", returning = "obj")  
  30.     public void checkReturn(JoinPoint joinPoint, Object obj){  
  31.         System.out.println("@AfterReturning-目标方法返回值:" + obj);  
  32.         System.out.println("@AfterReturning-生成日志");  
  33.           
  34.         System.out.println("@AfterReturning-目标对象为:" + joinPoint.getTarget());  
  35.         System.out.println("@AfterReturning-目标方法:" + joinPoint.getSignature().getName());  
  36.         System.out.println("@AfterReturning-目标方法的参数:" + Arrays.toString(joinPoint.getArgs()));  
  37.     }  
  38.       
  39.     @After("anyMethod()")  
  40.     public void checkAfter(JoinPoint joinPoint){  
  41.         System.out.println("@After-释放资源!");  
  42.           
  43.         System.out.println("@After-目标对象为:" + joinPoint.getTarget());  
  44.         System.out.println("@After-目标方法:" + joinPoint.getSignature().getName());  
  45.         System.out.println("@After-目标方法的参数:" + Arrays.toString(joinPoint.getArgs()));  
  46.     }  
  47.       
  48.     @Around("anyMethod()")  
  49.     public Object checkAround(ProceedingJoinPoint joinPoint) throws Throwable{  
  50.         System.out.println("@Around!");  
  51.           
  52.         Object[] args = joinPoint.getArgs();  
  53.         if(args != null && args.length > 0 && args[0].getClass() == String.class){  
  54.             args[0] = "@Around" + args[0];  
  55.         }  
  56.         Object result = joinPoint.proceed(args);  
  57.         if(result != null && result.getClass() == String.class){  
  58.             result = result + "--wy";  
  59.         }  
  60.         return result;  
  61.     }  
  62.       
  63.     @AfterThrowing(throwing = "ex", pointcut = "anyMethod()")  
  64.     public void checkAfterThrowing(Throwable ex){  
  65.         System.out.println("@AfterThrowing-异常抛出!");  
  66.         System.out.println("@AfterThrowing-异常日志!");  
  67.           
  68.     }  
  69.       
  70.       
  71. }  


配置文件需要将我们自己写的这个切面的类,在配置文件中进行“注册”。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:aop="http://www.springframework.org/schema/aop"  
  5.        xsi:schemaLocation="  
  6. http://www.springframework.org/schema/beans   
  7. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  8. http://www.springframework.org/schema/aop  
  9. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">  
  10.     <aop:aspectj-autoproxy/>  
  11.       
  12.     <bean id = "inter" class = "com.siti.spring20160315aop.MyInterceptor"></bean>  
  13.     <bean id = "wy" class = "com.siti.spring20160315aop.WangYang"></bean>  
  14. </beans>  
测试:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.siti.spring20160315aop;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class MainTest {  
  7.   
  8.     public static void main(String[] args) {  
  9.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext20160315.xml");  
  10.         Person wy = context.getBean("wy", Person.class);  
  11.         wy.say();  
  12.         wy.sayName("wangyang-");  
  13.         String str = wy.saySth("hello--");  
  14.         System.out.println("str的值:" + str);  
  15.         // wy.sayEx();// 抛出异常的测试  
  16.     }  
  17. }  




XML配置文件的方式


基于配置文件配置的方式和前面的注解的方式基本上类似的,这里只是将,改动的地方列出来,不做介绍了。

配置文件如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:aop="http://www.springframework.org/schema/aop"  
  5.        xsi:schemaLocation="  
  6. http://www.springframework.org/schema/beans   
  7. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  8. http://www.springframework.org/schema/aop  
  9. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">  
  10.       
  11.     <aop:config>  
  12.         <!-- 定义公共的切入点表达式 -->  
  13.         <aop:pointcut expression="execution(* com.siti.spring20160315aopwithxml.WangYang.*(..))" id="pointCutCommon"/>  
  14.         <!-- 定义切面,命名为 adviceRespect 优先级是2 ref:表示对谁进行增强处理-->  
  15.         <aop:aspect id = "adviceRespect" ref = "inter" order="2">  
  16.             <aop:before pointcut-ref="pointCutCommon" method="checkMessage"/>  
  17.             <aop:after-returning pointcut-ref="pointCutCommon" method="checkReturn" returning="obj"/>  
  18.             <aop:around pointcut-ref="pointCutCommon" method="checkAround"/>  
  19.             <aop:after pointcut-ref="pointCutCommon" method="checkAfter"/>  
  20.         </aop:aspect>  
  21.         <aop:aspect id = "firstExecute" ref = "inter" order = "1">  
  22.             <aop:after-throwing pointcut-ref="pointCutCommon" method="checkAfterThrowing" throwing="ex"/>  
  23.         </aop:aspect>   
  24.     </aop:config>  
  25.     <bean id = "inter" class = "com.siti.spring20160315aopwithxml.MyInterceptor"></bean>  
  26.     <bean id = "wy" class = "com.siti.spring20160315aopwithxml.WangYang"></bean>  
  27. </beans>  

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.siti.spring20160315aopwithxml;  
  2.   
  3. import java.util.Arrays;  
  4.   
  5. import org.aspectj.lang.JoinPoint;  
  6. import org.aspectj.lang.ProceedingJoinPoint;  
  7.   
  8. public class MyInterceptor {  
  9.   
  10.     public void anyMethod(){}  
  11.       
  12.     public void checkMessage(JoinPoint joinPoint){  
  13.         System.out.println("@Before-check!!!");  
  14.         System.out.println("@Before-目标对象为:" + joinPoint.getTarget());  
  15.         System.out.println("@Before-目标方法:" + joinPoint.getSignature().getName());  
  16.         System.out.println("@Before-目标方法的参数:" + Arrays.toString(joinPoint.getArgs()));  
  17.     }  
  18.       
  19.     public void checkReturn(JoinPoint joinPoint, Object obj){  
  20.         System.out.println("@AfterReturning-目标方法返回值:" + obj);  
  21.         System.out.println("@AfterReturning-生成日志");  
  22.           
  23.         System.out.println("@AfterReturning-目标对象为:" + joinPoint.getTarget());  
  24.         System.out.println("@AfterReturning-目标方法:" + joinPoint.getSignature().getName());  
  25.         System.out.println("@AfterReturning-目标方法的参数:" + Arrays.toString(joinPoint.getArgs()));  
  26.     }  
  27.       
  28.     public void checkAfter(JoinPoint joinPoint){  
  29.         System.out.println("@After-释放资源!");  
  30.           
  31.         System.out.println("@After-目标对象为:" + joinPoint.getTarget());  
  32.         System.out.println("@After-目标方法:" + joinPoint.getSignature().getName());  
  33.         System.out.println("@After-目标方法的参数:" + Arrays.toString(joinPoint.getArgs()));  
  34.     }  
  35.       
  36.     public Object checkAround(ProceedingJoinPoint joinPoint) throws Throwable{  
  37.         System.out.println("@Around!");  
  38.           
  39.         Object[] args = joinPoint.getArgs();  
  40.         if(args != null && args.length > 0 && args[0].getClass() == String.class){  
  41.             args[0] = "@Around" + args[0];  
  42.         }  
  43.         Object result = joinPoint.proceed(args);  
  44.         if(result != null && result.getClass() == String.class){  
  45.             result = result + "--wy";  
  46.         }  
  47.         return result;  
  48.     }  
  49.       
  50.     public void checkAfterThrowing(Throwable ex){  
  51.         System.out.println("@AfterThrowing-异常抛出!");  
  52.         System.out.println("@AfterThrowing-异常日志!");  
  53.           
  54.     }  
  55.       
  56. }  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值