spring AOP

来源:互联网 发布:程序员三宝github 编辑:程序博客网 时间:2024/05/29 17:00

在开始之前需要导入spring里面没有的二个包  

aopalliance.jar    aspectjweaver-1.5.0.jar   没有这二个包不能使用  


接口 


[java] view plain copy
  1. package com.my.aop;  
  2.   
  3. public interface IPerson {  
  4.       
  5.     public void add();  
  6.       
  7.     public void delete();  
  8.       
  9.     public void  update(int i);  
  10. }  


目标对象  
[java] view plain copy
  1. package com.my.aop;  
  2.   
  3. public class PersonImpl  implements IPerson{  
  4.   
  5.     @Override  
  6.     public void add() {  
  7.           
  8.         System.out.println("添加");  
  9.           
  10.         try {  
  11.             Thread.sleep(3000);  
  12.         } catch (InterruptedException e) {  
  13.             // TODO Auto-generated catch block  
  14.             e.printStackTrace();  
  15.         }  
  16.     }  
  17.   
  18.     @Override  
  19.     public void delete() {  
  20.       
  21.         System.out.println("删除");  
  22.           
  23.           
  24.     }  
  25.   
  26.     @Override  
  27.     public void update(int i) {  
  28.           
  29.         System.out.println("修改了");  
  30.           
  31.     }  
  32.   
  33. }  

aop  XML文件 


[html] view plain copy
  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:context="http://www.springframework.org/schema/context"  
  5.         xmlns:aop="http://www.springframework.org/schema/aop"  
  6.        xsi:schemaLocation="  
  7.         http://www.springframework.org/schema/beans   
  8.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  9.         http://www.springframework.org/schema/context   
  10.         http://www.springframework.org/schema/context/spring-context.xsd  
  11.         http://www.springframework.org/schema/aop  
  12.         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">  
  13.              
  14. <bean id="IPerson" class="com.my.aop.PersonImpl"></bean>  
  15. <bean id="logingAdvice" class="com.my.aop.advice.LogingAdvice"></bean>  
  16.   
  17. <!-- 配置一个aop  更多细节请查看API -->  
[html] view plain copy
  1. <aop:config>  
  2.    <aop:pointcut expression="execution(* com.my.aop.PersonImpl.add(..))" id="pointcut"/>  
  3.    <aop:pointcut expression="execution(* com.my.aop.PersonImpl.update(..))" id="pointcut1"/>  
  4.    <aop:advisor advice-ref="logingAdvice" pointcut-ref="pointcut"/>  
  5.    <aop:advisor advice-ref="logingAdvice" pointcut-ref="pointcut1"></aop:advisor>  
  6. </aop:config>  
  7.   
  8.   
  9.   
  10. </beans>             


AOP核心  

[java] view plain copy
  1. package com.my.aop.advice;  
  2.   
  3. import java.lang.reflect.Method;  
  4.   
  5.   
  6. import org.aopalliance.intercept.MethodInterceptor;  
  7. import org.aopalliance.intercept.MethodInvocation;  
  8. import org.springframework.aop.AfterReturningAdvice;  
  9. import org.springframework.aop.MethodBeforeAdvice;  
  10.   
  11. public class LogingAdvice  implements MethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor  {  
  12.   
  13.     @Override  
  14.     public void before(Method method, Object[] arg1, Object target)  
  15.             throws Throwable {  
  16.           
  17.         System.out.println( method.getName()+"方式被调用   目标对象"+target.getClass().getName());  
  18.           
  19.     }  
  20.   
  21.     @Override  
  22.     public void afterReturning(Object returnValue, Method method,  
  23.             Object[] args, Object target) throws Throwable {  
  24.           
  25.         System.out.println( method.getName()+"方式被执行后   目标对象"+target.getClass().getName());  
  26.     }  
  27.   
  28.     @Override  
  29.     public Object invoke(MethodInvocation method) throws Throwable {  
  30.           
  31.         System.out.println("环绕前====》》》》");  
  32.       
  33.         long start = System.currentTimeMillis();  
  34.       
  35.           
  36.         Object obj = method.proceed();  
  37.           
  38.           
  39.           
  40.         System.out.println("环绕后====》》》》");  
  41.           
  42.         long end = System.currentTimeMillis();  
  43.           
  44.         System.out.println("使用时间为:"+(end-start));  
  45.           
  46.         return obj;  
  47.     }  
  48.   
  49. }  

测试  


[java] view plain copy
  1. package com.my.test;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. import com.my.aop.IPerson;  
  7.   
  8. public class Test {  
  9.       
  10.     public static void main(String[] args) {  
  11.           
  12.         ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");  
  13.           
  14.         IPerson ip =  (IPerson) ac.getBean("IPerson");  
  15.           
  16.         ip.add();  
  17.           
  18.           
  19.     }  
  20.   
  21. }  
结果 


0 0
原创粉丝点击