论坛首页 入门技术论坛

spring aop 笔记2

浏览 3032 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-07-16  
spring aop的自动代理
BeanNameAutoProxyCreator为名字匹配字符串或者通配符的bean自动创建AOP代理。
java 代码
 
  1. public class AutoBean {  
  2.       
  3.     public void foo() {  
  4.         System.out.println("foo()");  
  5.     }  
  6. }  
通知
java 代码
 
  1. package aop;  
  2.   
  3. import java.lang.reflect.Method;  
  4.   
  5. import org.springframework.aop.MethodBeforeAdvice;  
  6.   
  7. public class SimpleBeforeAdvice implements MethodBeforeAdvice{  
  8.   
  9.     public void before(Method method, Object[] arg1, Object arg2) throws Throwable {  
  10.   
  11.         System.out.println("Bofore method:"+ method );  
  12.     }  
  13.   
  14. }  
配制
java 代码
 
  1. "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  2.    <beans>  </beans>
  3.          
  4.                      class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  5.                       
  6.                            <list>  </list>
  7.                            <value>foo*</value>  
  8.                           <value>barBean</value>  
  9.                             
  10.                                
  11.                        
  12.                            <list>  </list>
  13.                             <value>advice</value>  
  14.                              
  15.                               
  16.                               
  17.                  
  18.                   
  19.                 
  20.   
  21.             

test
java 代码
 
  1. package aop;  
  2.   
  3. import org.springframework.aop.support.AopUtils;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  6.   
  7. public class IntroductionConfigExample {  
  8.   
  9.     public static void main(String[] args) {  
  10.         ApplicationContext ctx = new FileSystemXmlApplicationContext(  
  11.                 "./src/beans.xml");  
  12.           
  13.         AutoBean autoBean = (AutoBean)ctx.getBean("fooBean");  
  14.         AutoBean autoBean1 = (AutoBean)ctx.getBean("barBean");  
  15. //        OtherBean otherBean = (OtherBean)ctx.getBean("otherBean");  
  16.           
  17.         autoBean.foo();  
  18.         System.out.println(AopUtils.isAopProxy(autoBean));  
  19.           
  20. //        otherBean.foo();  
  21. //        System.out.println(AopUtils.isAopProxy(otherBean));  
  22.   
  23.          
  24.     }  
  25. }             
  26.          

DefaultAdvisorAutoProxyCreator

一个更加通用而且强大得多的自动代理创建器是DefaultAdvisorAutoProxyCreator。它自动应用当前上下文中适当的advisor,无需在自动代理advisor的bean定义中包括bean的名字。 比起BeanNameAutoProxyCreator,它提供了同样关于一致性配置的优点而避免了前者的重复性。

可以向每个业务对象应用任意数量的advisor。对于一个业务对象,如果没有任何advisor中的切入点匹配它的任何方法,这个对象将不会被代理。

java 代码
 
  1. package aop2;  
  2.   
  3. public class AutoBean {  
  4.       
  5.     public void foo() {  
  6.         System.out.println("foo()");  
  7.     }  
  8. }  
java 代码
 
  1. package aop2;  
  2. public class OtherBean {  
  3.       
  4.     public void foo() {  
  5.         System.out.println("foo()");  
  6.     }  
  7. }  
java 代码
 
  1. package aop2;  
  2.   
  3. import java.lang.reflect.Method;  
  4.   
  5. import org.springframework.aop.MethodBeforeAdvice;  
  6.   
  7. public class SimpleBeforeAdvice implements MethodBeforeAdvice{  
  8.   
  9.     public void before(Method method, Object[] arg1, Object arg2) throws Throwable {  
  10.   
  11.         System.out.println("Bofore method:"+ method );  
  12.     }  
  13.   
  14. }  
定义pointcut
java 代码
 
  1. package aop2;  
  2.   
  3. import org.springframework.aop.ClassFilter;  
  4. import org.springframework.aop.MethodMatcher;  
  5. import org.springframework.aop.Pointcut;  
  6.       
  7. public class MyPointcut implements Pointcut {  
  8.       
  9.     public ClassFilter getClassFilter() {  
  10.         return new ClassFilter() {  
  11.             
  12.             public boolean matches(Class cls) {  
  13.                 return (cls == AutoBean.class);  //只代理autobean
  14.             }  
  15.         };  
  16.     }  
  17.       
  18.     public MethodMatcher getMethodMatcher() {  
  19.         return MethodMatcher.TRUE;  
  20.     }  
  21. }  
java 代码
 
  1. package aop2;  
  2.   
  3. import org.springframework.aop.support.AopUtils;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  6.   
  7. public class DefaultCreatorExample {  
  8.     public static void main(String[] args) {  
  9.         ApplicationContext ctx = new FileSystemXmlApplicationContext(  
  10.         "./src/beans1.xml");  
  11.           
  12.         AutoBean autoBean = (AutoBean)ctx.getBean("autoBean");  
  13.         OtherBean otherBean = (OtherBean)ctx.getBean("otherBean");  
  14.           
  15.         autoBean.foo();  
  16.         System.out.println(AopUtils.isAopProxy(autoBean));  
  17.           
  18.         otherBean.foo();  
  19.         System.out.println(AopUtils.isAopProxy(otherBean));  
  20.     }  
  21.   
  22. }  
配制

<beans><bean id="proxyCreator"><bean id="autoBean" class="aop2.AutoBean"><bean id="otherBean" class="aop2.OtherBean"><bean id="advisor"> <property name="pointcut">
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
   
    <bean id="proxyCreator">
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
   
    <bean id="autoBean" class="aop2.AutoBean">
    <bean id="otherBean" class="aop2.OtherBean">
   
    <bean id="advisor">
          class="org.springframework.aop.support.DefaultPointcutAdvisor">
           <property name="pointcut">
               <bean class="aop2.MyPointcut">
           </bean>
           <property name="advice">
               <bean class="aop2.SimpleBeforeAdvice">
           </bean>
    </property>
</property>

               <bean class="aop2.MyPointcut">
           </bean>
           <property name="advice">
               <bean class="aop2.SimpleBeforeAdvice">
           </bean>
    </property>
</bean> </bean></bean></bean></beans></property> </bean></bean></bean></bean></beans>
   发表时间:2007-07-16  
BeanNameAutoProxyCreators配制
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="proxyCreator"          
      class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames">
             <list>
                <value>foo*</value>
                <value>barBean</value>
            </list>
            </property>   
        <property name="interceptorNames">
            <list>
                <value>advice</value>
            </list>
        </property>
    </bean>
   
    <bean id="fooBean" class="aop.AutoBean"/>
    <bean id="barBean" class="aop.AutoBean"/>
   
    <bean id="advice" class="aop.SimpleBeforeAdvice"/>

</beans>
0 请登录后投票
   发表时间:2007-07-16  
DefaultAdvisorAutoProxyCreator配制
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
   
    <bean id="proxyCreator"
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
   
    <bean id="autoBean" class="aop2.AutoBean"/>
    <bean id="otherBean" class="aop2.OtherBean"/>
   
    <bean id="advisor"
          class="org.springframework.aop.support.DefaultPointcutAdvisor">
           <property name="pointcut">
               <bean class="aop2.MyPointcut"/>
           </property>
           <property name="advice">
               <bean class="aop2.SimpleBeforeAdvice"/>
           </property>
    </bean>
</beans>
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics