重生之毒妃安锦绣免费:Spring中的两种AOP织入方式

来源:百度文库 编辑:九乡新闻网 时间:2024/04/29 12:15:59
Spring实现aop是依赖两种技术,一种是jdk动态代理,被切入的类需要实现接口,如果在配置文件中不指明实现什么接口,spring会自动搜索其实现接口并织入advice,别一种是借助动态修改类的技术,使用cglib动态地扩展类来实现切面,cglib可以实现字节码级地修改,执行效率比jdk动态代理要高,但创建实例时没有前者快.默认情况下,使用jdk动态代理,通过下面的配置,可以显式指明到底使用哪种代理方式.起作用的是proxyTargetClass这个属性,为true的时候,代表要扩展织入的类,使用cglib,默认为false.如果你指定为false,却又没有实现接口,去调用方法的时候,一个Proxy$0不能转换之类的异常就会如约而至.
  view plaincopy to clipboardprint?
01. 
02.       
03.          
04.            transactionManager 
05.        
 
06.     
 
07.       
08.       
09.  
  
         另外一个使用aop很常见的问题,我认为是切点的定义,使用注解简单,但如果一个advice要切到很多地方,则要修改很多类,把它们加上注解,倒不如扩展一下StaticMethodMatcherPointcutAdvisor,通过实现match方法匹配切点,如:
  view plaincopy to clipboardprint?
01.package spring.aop; 
02. 
03. 
04.import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor; 
05. 
06.import java.lang.reflect.Method; 
07.import java.util.Arrays; 
08.import java.util.List; 
09. 
10.public class Advisor extends StaticMethodMatcherPointcutAdvisor{ 
11.    private static List classNames; 
12.    private static List  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,拦截器,配置文件:
  view plaincopy to clipboardprint?
01.package spring; 
02. 
03.public class Horse { 
04.    public void eat(String food) { 
05.        System.out.println("A horse is eating " + food); 
06.    } 
07.} 
  
view plaincopy to clipboardprint?
01.package spring; 
02. 
03. 
04.public class Person { 
05.    public void eat(String food) { 
06.        System.out.println("A person is eating " + food); 
07.    } 
08. 
09.    public void sleep(String name) { 
10.        System.out.println("Be quiet! " + name + " is sleeping"); 
11.    } 
12.} 
  
view plaincopy to clipboardprint?
01.package spring.aop; 
02. 
03. 
04.import org.aopalliance.intercept.MethodInterceptor; 
05.import org.aopalliance.intercept.MethodInvocation; 
06. 
07.public class AroundInterceptor implements MethodInterceptor { 
08.    public Object invoke(MethodInvocation invocation) throws Throwable { 
09.        System.out.println("intercepting " + invocation.getThis().getClass() + "#" + invocation.getMethod().getName()); 
10.        return invocation.proceed(); 
11.    } 
12.} 
  
view plaincopy to clipboardprint?
01.package spring; 
02. 
03. 
04.import org.springframework.context.ApplicationContext; 
05.import org.springframework.context.support.ClassPathXmlApplicationContext; 
06. 
07.public class Test { 
08.    public static void main(String[] args) { 
09.        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: view plaincopy to clipboardprint?
01. 
02.03.        "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> 
04. 
05. 
06.     
07.     
08. 
09.     
10.     
11.         
12.   
 
13. 
14.     
15.
 
 运行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包里,有了上述技术,解决起来轻而易举.
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/theoffspring/archive/2011/02/26/6209199.aspx