首页 > 代码库 > Spring AOP动态代理

Spring AOP动态代理

出现org.springframework.aop.framework.ProxyFactoryBean cannot be cast to 错误

  在类型转换的时候, 调用getObject()方法,再对ProxyFactoryBean进行转换

xml文件

  <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy><!--使用cglib动态代理-->    <bean id="greetingProxy" class="org.springframework.aop.framework.ProxyFactoryBean">        <property name="interfaces" value="http://www.mamicode.com/xxxx"></property><!--代理接口-->        <property name="target" ref="xxxx"></property><!--接口的实现类-->        <property name="interceptorNames" value="http://www.mamicode.com/greetingAroundAdvice"></property><!--引入增强-->    </bean>

 环绕增强类:

@Aspect@Componentpublic class GreetingAroundAdvice implements MethodInterceptor{    @Override    public Object invoke(MethodInvocation invocation) throws Throwable {        before();        Object result = invocation.proceed();        after();        return result;    }    private void before() {        System.out.println("before");    }        private void after() {        System.out.println("after");    }}

 

Spring AOP动态代理