如何在 Spring 配置文件中混合使用 CGLIB 和 JDK 代理?

2024-01-05

该线程与我遇到的一个问题有关这里关于访问建议类的受保护方法的需求 https://stackoverflow.com/q/10110253/827480。我正在使用 Spring 3.0.6,并创建了一个 Spring 分析方面,我将其应用于使用 JDK 代理的大量 bean。

然而,由于需要访问一个特定 bean 中的受保护方法,我建议使用 CGLIB。所有其他 bean 我想继续使用 JDK 代理。

我混合使用了注释和 xml 配置,但这个特定方面是在 XML 配置中定义的。

我知道有<aop:scoped-proxy>标签,但据我所知,这适用于所有方面。

是否有办法定义单个方面以使用 CGLIB 代替?

<aop:config>
    <aop:aspect id="Profiler" ref="lendingSimulationServiceProfilerInterceptor">
        <!-- info -->
        <aop:around method="infoProfiler"
                    pointcut="execution(* com.cws.cs.lendingsimulationservice.service.LendingSimulationServiceImpl.calculate*(..))"  />

        <!-- debug -->
        <aop:around method="infoProfiler"
                    pointcut="execution(* com.cws.cs.lendingsimulationservice.process.LendingSimulationProcessImpl.calculate(..))"  />

        <aop:around method="infoProfiler"
                    pointcut="execution(* com.blaze.BlazeEngine.invokeService(..))"  />

        <!-- trace -->
        <aop:around method="traceProfiler" 
                    pointcut="execution(* com.calculator.dao.impl.LendingSimulationDaoImpl.*(..))"  />

        <!-- NEED TO DEFINE THIS PARTICULAR ASPECT AS CGLIB -->
        <aop:around method="traceProfiler" 
                    pointcut="execution(* com.cws.cs.lendingsimulationservice.util.pool.JAXBPoolImpl.*(..))"    />
    </aop:aspect>
</aop:config>

我尝试将配置分成两部分,并为其中一个配置指定target-class="true"和另一个target-class="false",但此时似乎将 CGLIB 应用于所有情况。

有什么办法可以做到这一点吗?

Thanks,

Eric


不幸的是,要么所有 bean 使用 CGLIB,要么没有 bean 使用 CGLIB,如果您在一个地方使用目标类的代理,那么在所有其他地方都会强制使用它。引用8.6 代理机制 http://static.springsource.org/spring/docs/current/spring-framework-reference/html/aop.html#aop-proxying官方文档:

Note

多种的<aop:config/>各部分在运行时被折叠成一个统一的自动代理创建器,该创建器应用任何一个代理中最强的代理设置。<aop:config/>指定的部分(通常来自不同的 XML bean 定义文件)。这也适用于<tx:annotation-driven/> and <aop:aspectj-autoproxy/>元素。

需要明确的是:使用'proxy-target-class="true"' on <tx:annotation-driven/>, <aop:aspectj-autoproxy/> or <aop:config/>元素将强制使用 CGLIB 代理对于他们三个人来说.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Spring 配置文件中混合使用 CGLIB 和 JDK 代理? 的相关文章

随机推荐