Spring 顾问 自动代理

[java]  view plain  copy
  1. public interface IDog1 {  
  2.     public  void  dog();  
  3.     public  void  run();  
  4. }  
  5. public class MyAfter implements AfterReturningAdvice {  
  6.     public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {  
  7.         System.out.println("=====after===");  
  8.     }  
  9. }  
  10. public class Dog1 implements IDog1 {  
  11.     public  void  dog(){  
  12.         System.out.println("===测试顾问==1dog()");  
  13.     }  
  14.   
  15.     public void run() {  
  16.         System.out.println("====测试顾问===2run()");  
  17.     }  
  18. }  
  19. public class MyBefore1 implements MethodBeforeAdvice {  
  20.     public void before(Method method, Object[] objects, Object o) throws Throwable {  
  21.         System.out.println("======before====");  
  22.     }  
  23. }  

[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.   
  5.        xmlns:aop="http://www.springframework.org/schema/aop"  
  6.   
  7.        xmlns:context="http://www.springframework.org/schema/context"  
  8.   
  9.        xmlns:p="http://www.springframework.org/schema/p"  
  10.   
  11.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  12.        http://www.springframework.org/schema/beans/spring-beans.xsd  
  13.   
  14.        http://www.springframework.org/schema/aop  
  15.        http://www.springframework.org/schema/aop/spring-aop.xsd  
  16.   
  17.        http://www.springframework.org/schema/context  
  18.        http://www.springframework.org/schema/context/spring-context.xsd  
  19.  ">  
  20.   
  21.    <bean id="dog" class="cn.springGuWenZiDongDaiLi.Dog1"></bean>  
  22.   
  23.     <bean id="before" class="cn.springGuWenZiDongDaiLi.MyBefore1"></bean>  
  24.   
  25.     <bean id="aftet" class="cn.springGuWenZiDongDaiLi.MyAfter"></bean>  
  26.   
  27.       <!--正则 表达式匹配-->  
  28.     <!--<bean id="GuWen" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">  
  29.         <property name="advice" ref="before"></property>  
  30.         <property name="patterns" value=".*do.*"></property>  
  31.     </bean>  
  32.     <bean id="GuWen2" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">  
  33.         <property name="advice" ref="aftet"></property>  
  34.         <property name="patterns" value=".*do.*"></property>  
  35.     </bean>-->  
  36.   
  37.     <!--自动代理  缺点只能代理 顾问 模式-->  
  38.     <!--<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>-->  
  39.   
  40.   
  41.     <!--自动代理  代理顾问 也可以代理 通知-->  
  42. <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  43.     <property name="beanNames" value="dog"></property>  
  44.     <property name="interceptorNames" value="aftet"></property>  
  45. </bean>  
  46.   
  47.   
  48.   
  49. </beans>  
[html]  view plain  copy
  1. //顾问 自动 代理  
  2.     @Test  
  3.     public  void  aVoid2(){  
  4.         ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext08GuWenZiDongDaiLi.xml");  
  5.         IDog1 dog = (IDog1)ctx.getBean("dog");  
  6.         dog.dog();  
  7.         System.out.println();  
  8.         dog.run();  
  9.     }  

猜你喜欢

转载自blog.csdn.net/qq_36074218/article/details/76438251