Spring中的两种AOP织入方式

来源:互联网 发布:php 开源 api文档工具 编辑:程序博客网 时间:2024/06/06 08:42

 Spring实现aop是依赖两种技术,一种是jdk动态代理,被切入的类需要实现接口,如果在配置文件中不指明实现什么接口,spring会自动搜索其实现接口并织入advice,别一种是借助动态修改类的技术,使用cglib动态地扩展类来实现切面,cglib可以实现字节码级地修改,执行效率比jdk动态代理要高,但创建实例时没有前者快.默认情况下,使用jdk动态代理,通过下面的配置,可以显式指明到底使用哪种代理方式.起作用的是proxyTargetClass这个属性,为true的时候,代表要扩展织入的类,使用cglib,默认为false.如果你指定为false,却又没有实现接口,去调用方法的时候,一个Proxy$0不能转换之类的异常就会如约而至.

[xhtml] view plaincopy
  1. <bean id="forumService" class="org.springframework.aop.framework.ProxyFactoryBean">  
  2.       <property name="interceptorNames">  
  3.          <list>  
  4.             <value>transactionManager</value>  
  5.          </list>  
  6.       </property>  
  7.       <property name="target" ref="forumServiceTarget"/>  
  8.       <property name="proxyTargetClass" value="true"/>  
  9.    </bean>   

   

    另外一个使用aop很常见的问题,我认为是切点的定义,使用注解简单,但如果一个advice要切到很多地方,则要修改很多类,把它们加上注解,倒不如扩展一下StaticMethodMatcherPointcutAdvisor,通过实现match方法匹配切点,如:

[java] view plaincopy
  1. package spring.aop;  
  2.   
  3.   
  4. import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;  
  5.   
  6. import java.lang.reflect.Method;  
  7. import java.util.Arrays;  
  8. import java.util.List;  
  9.   
  10. public class Advisor extends StaticMethodMatcherPointcutAdvisor{  
  11.     private static List<String> classNames;  
  12.     private static List<String>  methodNames;  
  13.     static {  
  14.         classNames = Arrays.asList("spring.Person","spring.Horse");  
  15.         methodNames=Arrays.asList("eat");  
  16.     }  
  17.     public boolean matches(Method method, Class<?> targetClass) {  
  18.         return  classNames.contains(targetClass.getName()) && methodNames.contains(method.getName());  
  19.     }  
  20. }  

 

其它类,bean,拦截器,配置文件:

[java] view plaincopy
  1. package spring;  
  2.   
  3. public class Horse {  
  4.     public void eat(String food) {  
  5.         System.out.println("A horse is eating " + food);  
  6.     }  
  7. }  

[java] view plaincopy
  1. package spring;  
  2.   
  3.   
  4. public class Person {  
  5.     public void eat(String food) {  
  6.         System.out.println("A person is eating " + food);  
  7.     }  
  8.   
  9.     public void sleep(String name) {  
  10.         System.out.println("Be quiet! " + name + " is sleeping");  
  11.     }  
  12. }  

[java] view plaincopy
  1. package spring.aop;  
  2.   
  3.   
  4. import org.aopalliance.intercept.MethodInterceptor;  
  5. import org.aopalliance.intercept.MethodInvocation;  
  6.   
  7. public class AroundInterceptor implements MethodInterceptor {  
  8.     public Object invoke(MethodInvocation invocation) throws Throwable {  
  9.         System.out.println("intercepting " + invocation.getThis().getClass() + "#" + invocation.getMethod().getName());  
  10.         return invocation.proceed();  
  11.     }  
  12. }  

[java] view plaincopy
  1. package spring;  
  2.   
  3.   
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. public class Test {  
  8.     public static void main(String[] args) {  
  9.         ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");  
  10.   
  11.         Horse horse = (Horse) ctx.getBean("horse");  
  12.         Person person = (Person) ctx.getBean("person");  
  13.   
  14.         horse.eat("grass");  
  15.         person.eat("meat");  
  16.   
  17.         person.sleep("theoffspring");  
  18.   
  19.     }  
  20. }  

 

applicationContext.xml:

[java] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"  
  3.         "http://www.springframework.org/dtd/spring-beans-2.0.dtd">  
  4.   
  5. <beans>  
  6.     <bean id="horse" class="spring.Horse"/>  
  7.     <bean id="person" class="spring.Person"/>  
  8.   
  9.     <bean id="advice" class="spring.aop.AroundInterceptor"/>  
  10.     <bean id="advisor" class="spring.aop.Advisor">  
  11.         <property name="advice" ref="advice"/>  
  12.     </bean>  
  13.   
  14.     <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>  
  15. </beans>  

 

运行Test测试类,结果如下,证明everything is ok,切点是只拦截horse和person类的eat方法:

intercepting class spring.Horse#eat
A horse is eating grass
intercepting class spring.Person#eat
A person is eating meat
Be quiet! theoffspring is sleeping

    

    最后要提一下,一定要使用自动代理技术,这里使用的是DefaultAdvisorAutoProxyCreator,它会自动搜索符合切点的所有spring bean进行织入,极其方便.以免手工为每个bean创建织入的配置.

    目前我在公司做的一个监控项目需要对几十个运行着的工程进行数据拦截,自动同步到另一个数据库中去,涉及的action,业务方法不计其数,有的业务类是别的部门开发的,我们无权修改,打到公共jar包里,有了上述技术,解决起来轻而易举.

0 0
原创粉丝点击