Spring实现aop是依赖两种技术,一种是jdk动态代理,被切入的类需要实现接口,如果在配置文件中不指明实现什么接口,spring会自动搜索其实现接口并织入advice,别一种是借助动态修改类的技术,使用cglib动态地扩展类来实现切面,cglib可以实现字节码级地修改,执行效率比jdk动态代理要高,但创建实例时没有前者快.默认情况下,使用jdk动态代理,通过下面的配置,可以显式指明到底使用哪种代理方式.起作用的是proxyTargetClass这个属性,为true的时候,代表要扩展织入的类,使用cglib,默认为false.如果你指定为false,却又没有实现接口,去调用方法的时候,一个Proxy$0不能转换之类的异常就会如约而至.
- <bean id="forumService" class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="interceptorNames">
- <list>
- <value>transactionManager</value>
- </list>
- </property>
- <property name="target" ref="forumServiceTarget"/>
- <property name="proxyTargetClass" value="true"/>
- </bean>
另外一个使用aop很常见的问题,我认为是切点的定义,使用注解简单,但如果一个advice要切到很多地方,则要修改很多类,把它们加上注解,倒不如扩展一下StaticMethodMatcherPointcutAdvisor,通过实现match方法匹配切点,如:
- package spring.aop;
- import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
- import java.lang.reflect.Method;
- import java.util.Arrays;
- import java.util.List;
- public class Advisor extends StaticMethodMatcherPointcutAdvisor{
- private static List<String> classNames;
- private static List<String> methodNames;
- static {
- classNames = Arrays.asList("spring.Person","spring.Horse");
- methodNames=Arrays.asList("eat");
- }
- public boolean matches(Method method, Class<?> targetClass) {
- return classNames.contains(targetClass.getName()) && methodNames.contains(method.getName());
- }
- }
其它类,bean,拦截器,配置文件:
- package spring;
- public class Horse {
- public void eat(String food) {
- System.out.println("A horse is eating " + food);
- }
- }
- package spring;
- public class Person {
- public void eat(String food) {
- System.out.println("A person is eating " + food);
- }
- public void sleep(String name) {
- System.out.println("Be quiet! " + name + " is sleeping");
- }
- }
- package spring.aop;
- import org.aopalliance.intercept.MethodInterceptor;
- import org.aopalliance.intercept.MethodInvocation;
- public class AroundInterceptor implements MethodInterceptor {
- public Object invoke(MethodInvocation invocation) throws Throwable {
- System.out.println("intercepting " + invocation.getThis().getClass() + "#" + invocation.getMethod().getName());
- return invocation.proceed();
- }
- }
- package spring;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Test {
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
- Horse horse = (Horse) ctx.getBean("horse");
- Person person = (Person) ctx.getBean("person");
- horse.eat("grass");
- person.eat("meat");
- person.sleep("theoffspring");
- }
- }
applicationContext.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
- "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
- <beans>
- <bean id="horse" class="spring.Horse"/>
- <bean id="person" class="spring.Person"/>
- <bean id="advice" class="spring.aop.AroundInterceptor"/>
- <bean id="advisor" class="spring.aop.Advisor">
- <property name="advice" ref="advice"/>
- </bean>
- <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
- </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包里,有了上述技术,解决起来轻而易举.