小编Thi*_*ini的帖子

在Spring中混合使用JDK和CGLIB代理

我有一个运行Spring的应用程序,我在某些地方使用AOP.由于我想在接口级别使用@Transactional注释,我必须允许Spring创建JDK代理.所以,我没有将proxy-target-class属性设置为true.另一方面,我不想为我想要的每个类创建一个接口:如果接口没有意义,我想只有实现,Spring应该创建一个CGLIB代理.

就像我描述的那样,一切都很完美.但我希望在接口中有一些其他的注释(由我创建),并由实现类"继承"(就像@Transactional一样).事实证明,我无法通过Spring中对AOP的内置支持来做到这一点(至少我无法弄清楚在经过一些研究后如何做到这一点.接口中的注释在实现类中是不可见的,并且因此,该课程没有得到建议).

所以我决定实现自己的切入点拦截器,允许其他方法注释进入接口.基本上,我的切入点查找方法上的注释,直到找不到,在类或其超类实现的接口的相同方法(相同名称和参数类型)中.

问题是:当我声明一个DefaultAdvisorAutoProxyCreator bean,它将正确应用这个切入点/拦截器时,建议没有接口的类的行为被打破.显然出现了问题,Spring尝试两次代理我的类,一次使用CGLIB,然后使用JDK.

这是我的配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

    <!-- Activates various annotations to be detected in bean classes: Spring's 
        @Required and @Autowired, as well as JSR 250's @Resource. -->
    <context:annotation-config />

    <context:component-scan base-package="mypackage" />

    <!-- Instruct Spring to perform declarative transaction management automatically 
        on annotated classes. -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />

    <bean id="logger.advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
        <constructor-arg>
            <bean …
Run Code Online (Sandbox Code Playgroud)

aop spring spring-aop cglib

16
推荐指数
2
解决办法
8308
查看次数

标签 统计

aop ×1

cglib ×1

spring ×1

spring-aop ×1