Spring-------- 增強

spring的增強分類:前置增強,後置增強,異常增強,環繞增強。

增強的意義在於我們不改變接口,實現類,不動方法的前提下在原內容上增加內容。

首先我們看一下前置增強:前置增強必須實現的接口:MethodBeforeAdvice

[java] view plain copy
 
 
  1. MethodBeforeAdvice  

[java] view plain copy
 
 
  1. package cn.happy.spring11;  
  2.   
  3. /** 
  4.  * Created by lihuohuo on 2017/7/31. 
  5.  */  
  6. public interface IDoSome {  
  7.     public void add();  
  8.     public void add1();  
  9.     public void add2();  
  10.   
  11.   
  12. }  

[java] view plain copy
 
 
  1. package cn.happy.spring11;  
  2.   
  3. /** 
  4.  * Created by lihuohuo on 2017/7/31. 
  5.  */  
  6. public class IDoSomes implements IDoSome{  
  7.     public void add() {  
  8.         System.out.println("=========1=======");  
  9.     }  
  10.     public void add1() {  
  11.         System.out.println("=========2=======");  
  12.     }public void add2() {  
  13.         System.out.println("=========3=======");  
  14.     }  
  15.   
  16. }  

[java] view plain copy
 
 
  1. package cn.happy.spring11;  
  2.   
  3. import org.springframework.aop.MethodBeforeAdvice;  
  4.   
  5. import java.lang.reflect.Method;  
  6.   
  7. /** 
  8.  * Created by lihuohuo on 2017/7/31. 
  9.  */  
  10. public class MyBeforeAdvice implements MethodBeforeAdvice{  
  11.     public void before(Method method, Object[] objects, Object o) throws Throwable {  
  12.         System.out.println("=========before=======");  
  13.     }  
  14. }  

[java] view plain copy
 
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:aop="http://www.springframework.org/schema/aop"  
  4.        xmlns:p="http://www.springframework.org/schema/p"  
  5.        xmlns:context="http://www.springframework.org/schema/context"  
  6.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
  9.    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">  
  10. <bean id="Idosome" class="cn.happy.spring11.IDoSomes"></bean>  
  11.     <bean id="beforeAdvice" class="cn.happy.spring11.MyBeforeAdvice"></bean>  
  12. <bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">  
  13.     <property name="target" ref="Idosome"></property>  
  14.     <property name="interceptorNames" value="beforeAdvice"></property>  
  15.   
  16. </bean>  
  17.   
  18. </beans>  

[java] view plain copy
 
 
  1. @Test  
  2.   public void test01(){  
  3.       ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext07.xml");  
  4.       IDoSome i=(IDoSome)ctx.getBean("someProxy");  
  5.       i.add();  
  6.       i.add1();  
  7.       i.add2();  
  8.   }  




下面是後置增強:實現的接口是:AfterReturningAdvice
[java] view plain copy
 
 
  1. package cn.happy.spring12;  
  2.   
  3. /** 
  4.  * Created by lihuohuo on 2017/7/31. 
  5.  */  
  6. public interface IDoSome {  
  7.     public void add();  
  8.     public String add1();  
  9.     public void add2();  
  10.   
  11.   
  12. }  

[java] view plain copy
 
 
  1. package cn.happy.spring12;  
  2.   
  3. /** 
  4.  * Created by lihuohuo on 2017/7/31. 
  5.  */  
  6. public class IDoSomes implements IDoSome {  
  7.     public void add() {  
  8.         System.out.println("=========1=======");  
  9.     }  
  10.     public String add1() {  
  11.         System.out.println("=========2=======");  
  12.         return "呵呵abc";  
  13.     }  
  14.   
  15.     public void add2() {  
  16.         System.out.println("=========3=======");  
  17.     }  
  18.   
  19. }  

[java] view plain copy
 
 
  1. package cn.happy.spring12;  
  2.   
  3. import org.springframework.aop.AfterReturningAdvice;  
  4. import org.springframework.aop.MethodBeforeAdvice;  
  5.   
  6. import java.lang.reflect.Method;  
  7.   
  8. /** 
  9.  * Created by lihuohuo on 2017/7/31. 
  10.  */  
  11. public class MyAfterAdvice implements AfterReturningAdvice{  
  12.   
  13.     public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {  
  14.         System.out.println("========after=======");  
  15.         if(o!=null){  
  16.             System.out.println(o.toString().toUpperCase());  
  17.         }  
  18.     }  
  19. }  


[java] view plain copy
 
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:aop="http://www.springframework.org/schema/aop"  
  4.        xmlns:p="http://www.springframework.org/schema/p"  
  5.        xmlns:context="http://www.springframework.org/schema/context"  
  6.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
  9.    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">  
  10. <bean id="Idosome" class="cn.happy.spring12.IDoSomes"></bean>  
  11.     <bean id="afterAdvice" class="cn.happy.spring12.MyAfterAdvice"></bean>  
  12. <bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">  
  13.     <property name="target" ref="Idosome"></property>  
  14.     <property name="interceptorNames" value="afterAdvice"></property>  
  15.   
  16. </bean>  
  17.   
  18. </beans>  


[java] view plain copy
 
 
  1. @Test  
  2.    public void test02(){  
  3.        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext08.xml");  
  4.        cn.happy.spring12.IDoSome i=(cn.happy.spring12.IDoSome)ctx.getBean("someProxy");  
  5.        i.add();  
  6.       String id=i.add1();  
  7.        System.out.println(id);  
  8.        i.add2();  
  9.    }  


下面是環繞增強:實現接口是:MethodInterceptor 

[java] view plain copy
 
 
  1. package cn.happy.spring13;  
  2.   
  3. /** 
  4.  * Created by lihuohuo on 2017/7/31. 
  5.  */  
  6. public interface IDoSome {  
  7.     public void add();  
  8.     public String add1();  
  9.     public void add2();  
  10.   
  11.   
  12. }  

[java] view plain copy
 
 
  1. package cn.happy.spring13;  
  2.   
  3. /** 
  4.  * Created by lihuohuo on 2017/7/31. 
  5.  */  
  6. public class IDoSomes implements IDoSome {  
  7.     public void add() {  
  8.         System.out.println("=========1=======");  
  9.     }  
  10.     public String add1() {  
  11.         System.out.println("=========2=======");  
  12.         return "呵呵abc";  
  13.     }  
  14.   
  15.     public void add2() {  
  16.         System.out.println("=========3=======");  
  17.     }  
  18.   
  19. }  

[java] view plain copy
 
 
  1. package cn.happy.spring13;  
  2.   
  3. import org.aopalliance.intercept.MethodInterceptor;  
  4. import org.aopalliance.intercept.MethodInvocation;  
  5.   
  6. /** 
  7.  * Created by lihuohuo on 2017/7/31. 
  8.  */  
  9. public class MethodAdvice implements MethodInterceptor {  
  10.     public Object invoke(MethodInvocation methodInvocation) throws Throwable {  
  11.         System.out.println("之前============");  
  12.        Object result=methodInvocation.proceed();  
  13.        String temp=null;  
  14.        if(result!=null){  
  15.            temp=(String)result;  
  16.            temp=temp.toUpperCase();  
  17.        }  
  18.         System.out.println("之後================");  
  19.         System.out.println("======temp========="+temp);  
  20.         return temp;  
  21.     }  
  22. }  


[java] view plain copy
 
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:aop="http://www.springframework.org/schema/aop"  
  4.        xmlns:p="http://www.springframework.org/schema/p"  
  5.        xmlns:context="http://www.springframework.org/schema/context"  
  6.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
  9.    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">  
  10. <bean id="Idosome" class="cn.happy.spring13.IDoSomes"></bean>  
  11.     <bean id="methodAdvice" class="cn.happy.spring13.MethodAdvice"></bean>  
  12. <bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">  
  13.     <property name="target" ref="Idosome"></property>  
  14.     <property name="interceptorNames" value="methodAdvice"></property>  
  15.   
  16. </bean>  
  17.   
  18. </beans>  



[java] view plain copy
 
 
  1. @Test  
  2.   public void test03(){  
  3.       ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext09.xml");  
  4.       cn.happy.spring13.IDoSome i=(cn.happy.spring13.IDoSome)ctx.getBean("someProxy");  
  5.       i.add();  
  6.       String id=i.add1();  
  7.       System.out.println(id);  
  8.       i.add2();  
  9.   }  


最後一個是異常增強:實現接口是:ThrowsAdvice

[java] view plain copy
 
 
  1. package cn.happy.spring14;  
  2.   
  3. /** 
  4.  * Created by lihuohuo on 2017/7/31. 
  5.  */  
  6. public interface IDoSome {  
  7.     public void add() throws UserException;  
  8.     public String add1();  
  9.     public void add2();  
  10.   
  11.   
  12. }  

[java] view plain copy
 
 
  1. package cn.happy.spring14;  
  2.   
  3. /** 
  4.  * Created by lihuohuo on 2017/7/31. 
  5.  */  
  6. public class IDoSomes implements IDoSome {  
  7.     public void add() throws UserException {  
  8.         System.out.println("=========1=======");  
  9.         throw new UserException("總算有錯了");  
  10.     }  
  11.     public String add1() {  
  12.         System.out.println("=========2=======");  
  13.         return "呵呵abc";  
  14.     }  
  15.   
  16.     public void add2() {  
  17.         System.out.println("=========3=======");  
  18.     }  
  19.   
  20. }  

[java] view plain copy
 
 
  1. package cn.happy.spring14;  
  2.   
  3. import org.springframework.aop.ThrowsAdvice;  
  4.   
  5. /** 
  6.  * Created by lihuohuo on 2017/7/31. 
  7.  */  
  8. public class MyThrowsAdvice implements ThrowsAdvice{  
  9.     public void afterThrowing(UserException ex){  
  10.         System.out.println("錯誤提示:"+ex.getMessage());  
  11.     }  
  12.   
  13. }  

[java] view plain copy
 
 
  1. package cn.happy.spring14;  
  2.   
  3. /** 
  4.  * Created by lihuohuo on 2017/7/31. 
  5.  */  
  6. public class UserException extends Exception{  
  7.     public UserException() {  
  8.         super();  
  9.     }  
  10.   
  11.     public UserException(String message) {  
  12.         super(message);  
  13.     }  
  14. }  

[java] view plain copy
 
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:aop="http://www.springframework.org/schema/aop"  
  4.        xmlns:p="http://www.springframework.org/schema/p"  
  5.        xmlns:context="http://www.springframework.org/schema/context"  
  6.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
  9.    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">  
  10. <bean id="Idosome" class="cn.happy.spring14.IDoSomes"></bean>  
  11.     <bean id="throwsAdvice" class="cn.happy.spring14.MyThrowsAdvice"></bean>  
  12. <bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">  
  13.     <property name="target" ref="Idosome"></property>  
  14.     <property name="interceptorNames" value="throwsAdvice"></property>  
  15.   
  16. </bean>  
  17.   
  18. </beans>  

[java] view plain copy
 
 
  1. @Test  
  2.   public void test04(){  
  3.       ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext10.xml");  
  4.       cn.happy.spring14.IDoSome i=(cn.happy.spring14.IDoSome)ctx.getBean("someProxy");  
  5.       try {  
  6.           i.add();  
  7.       } catch (UserException e) {  
  8.           e.printStackTrace();  
  9.       }  
  10.   }  


還有2種配置文件改變 方法的前置增強

[java] view plain copy
 
 
  1. package cn.happy.spring16;  
  2.   
  3. /** 
  4.  * Created by lihuohuo on 2017/7/31. 
  5.  */  
  6. public interface IDoSome {  
  7.     public void adTd();  
  8.     public void adTd1();  
  9.     public void add2();  
  10.   
  11.   
  12. }  

[java] view plain copy
 
 
  1. package cn.happy.spring16;  
  2.   
  3. /** 
  4.  * Created by lihuohuo on 2017/7/31. 
  5.  */  
  6. public class IDoSomes implements IDoSome {  
  7.     public void adTd() {  
  8.         System.out.println("=========1=======");  
  9.     }  
  10.     public void adTd1() {  
  11.         System.out.println("=========2=======");  
  12.     }  
  13.     public void add2() {  
  14.         System.out.println("=========3=======");  
  15.     }  
  16.   
  17. }  

[java] view plain copy
 
 
  1. package cn.happy.spring16;  
  2.   
  3. import org.springframework.aop.MethodBeforeAdvice;  
  4.   
  5. import java.lang.reflect.Method;  
  6.   
  7. /** 
  8.  * Created by lihuohuo on 2017/7/31. 
  9.  */  
  10. public class MyBeforeAdvice implements MethodBeforeAdvice{  
  11.     public void before(Method method, Object[] objects, Object o) throws Throwable {  
  12.         System.out.println("=========before=======");  
  13.     }  
  14. }  


第一種使用方法名字 name 對特定的方法進行增強:

[java] view plain copy
 
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:aop="http://www.springframework.org/schema/aop"  
  4.        xmlns:p="http://www.springframework.org/schema/p"  
  5.        xmlns:context="http://www.springframework.org/schema/context"  
  6.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
  9.    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">  
  10. <bean id="Idosome" class="cn.happy.spring15.IDoSomes"></bean>  
  11.     <bean id="beforeAdvice" class="cn.happy.spring15.MyBeforeAdvice"></bean>  
  12.     <bean id="beforeAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">  
  13.         <property name="advice" ref="beforeAdvice"></property>  
  14.         <property name="mappedNames" value="add"></property>  
  15.   
  16.     </bean>  
  17.   
  18. <bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">  
  19.     <property name="target" ref="Idosome"></property>  
  20.     <property name="interceptorNames" value="beforeAdvisor"></property>  
  21.   
  22. </bean>  
  23.   
  24. </beans>  


第二種 根據名的 .*T*.的這種方法

[java] view plain copy
 
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:aop="http://www.springframework.org/schema/aop"  
  4.        xmlns:p="http://www.springframework.org/schema/p"  
  5.        xmlns:context="http://www.springframework.org/schema/context"  
  6.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
  9.    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">  
  10. <bean id="Idosome" class="cn.happy.spring16.IDoSomes"></bean>  
  11.     <bean id="beforeAdvice" class="cn.happy.spring16.MyBeforeAdvice"></bean>  
  12.     <bean id="beforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">  
  13.         <property name="advice" ref="beforeAdvice"></property>  
  14.         <property name="patterns" value=".*T.*"></property>  
  15.   
  16.     </bean>  
  17.   
  18. <bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">  
  19.     <property name="target" ref="Idosome"></property>  
  20.     <property name="interceptorNames" value="beforeAdvisor"></property>  
  21. <property name="proxyTargetClass" value="true"></property>  
  22. </bean>  
  23.   
  24. </beans>  


下面是測試類 ,都是一樣的,。

[java] view plain copy
 
 
  1. @Test  
  2. public void test06(){  
  3.     ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext12.xml");  
  4.     cn.happy.spring16.IDoSome i=(cn.happy.spring16.IDoSome)ctx.getBean("someProxy");  
  5.     i.add2();  
  6.   i.adTd();  
  7.   i.adTd1();  
發佈了184 篇原創文章 · 獲贊 62 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章