日期:2014-05-20  浏览次数:20673 次

spring AOP 拦截action问题
我在spring文档中看到:spring缺省使用JDK提供的动态代理类来实现AOP,当目标类没有接口的时候会自动使用cglib,我在实际中遇到一点问题,这是我的配置:
XML code

<aop:aspectj-autoproxy  proxy-target-class="true"/>


这样配置时可以正确拦截到action,当我把配置中的proxy配置项去掉时就拦截不到了,如下:
XML code

<aop:aspectj-autoproxy>



这样就无法拦截到action了,我在网上查了查说
XML code

 proxy-target-class="true"


的配置时配置强制使用cglib。我现在不想配置他,也就是让有接口的时候用jdk的,没有接口的时候使用cglib,为什么不行?


------解决方案--------------------
友情帮顶,你查阅的资料是不是有点问题:
<xsd:attribute name="proxy-target-class" type="xsd:boolean" default="true">
<xsd:annotation>
<xsd:documentation>
Are class-based (CGLIB) proxies to be created? This is the default; in order to
switch to standard Java interface-based proxies, turn this flag to "false".
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
------解决方案--------------------
你的是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>