我在spring文档中看到:spring缺省使用JDK提供的动态代理类来实现AOP,当目标类没有接口的时候会自动使用cglib,我在实际中遇到一点问题,这是我的配置:<aop:aspectj-autoproxy  proxy-target-class="true"/>这样配置时可以正确拦截到action,当我把配置中的proxy配置项去掉时就拦截不到了,如下:<aop:aspectj-autoproxy>
这样就无法拦截到action了,我在网上查了查说 proxy-target-class="true"的配置时配置强制使用cglib。我现在不想配置他,也就是让有接口的时候用jdk的,没有接口的时候使用cglib,为什么不行?

解决方案 »

  1.   

    你的是spring哪个版本,我用的是spring2.5的
    据我所知,假如你现在有个UserInterface  然后你有个UserImpl实现类
    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><bean id="advice" class="advice.Advice"></bean><bean id="target" class="serviceimpl.UserImpl"></bean>
    <!--interceptorNames 拦截器的名称 你要加入哪些通知 -->
    <bean id="userService"  
     class="org.springframework.aop.framework.ProxyFactoryBean">
           这个interfaces不写也可以,如果写了的话是要有接口实现的
    <property name="interfaces">
    <list>
    <value>service.UesrDAO</value>
    </list>
    </property>
    <property name="interceptorNames">
    <list>
    <value>advice</value>
    </list>
    </property>
    <property name="target" ref="target"></property>
    </bean>
      

  2.   

    呵呵,谢谢楼上两位,我解决问题了,分享给大家:
    spring3 默认使用JDK动态代理来实现AOP,如果struts2使用的动态方法调用,则spring3只能代理实现了接口的类,而不能代理action控制器,所以必须得这样了: <aop:aspectj-autoproxy proxy-target-class="true"/>