简体   繁体   English

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

[英]How to mix CGLIB and JDK proxies in Spring configuration files?

This thread is related to a problem I am encountering here regarding the needs for access to protected methods of an advised class . 这个线程与我在这里遇到的有关访问受建议类的受保护方法的需求有关 I'm using Spring 3.0.6, and have created a Spring profiling aspect that I am applying to a significant number of beans using JDK proxies. 我正在使用Spring 3.0.6,并创建了一个Spring分析方面,我正在使用JDK代理应用于大量的bean。

However, due to the need to access protected methods in one particular bean, I would like to advise it using CGLIB. 但是,由于需要访问一个特定bean中的受保护方法,我想使用CGLIB建议它。 All other beans I would like to continue to use JDK Proxies. 所有其他bean我想继续使用JDK Proxies。

I am using a mix of annotations and xml configuration, but this particular aspect is defined in XML configuration. 我使用了注释和xml配置的混合,但这个特定方面是在XML配置中定义的。

I know that there is <aop:scoped-proxy> tag, but from what I can tell, that applies to all aspects. 我知道有<aop:scoped-proxy>标签,但据我所知,这适用于所有方面。

Is there anyway to define for a single aspect to use CGLIB instead? 无论如何要定义单个方面来使用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>

I've tried to split the configuration into two, and for one configuration specify target-class="true" and the other target-class="false" , but it seems to apply CGLIB to all at that point. 我试图将配置拆分为两个,并且对于一个配置指定target-class="true"而另一个target-class="false" ,但它似乎在那时将CGLIB应用于所有人。

Is there any way to accomplish this? 有没有办法实现这个目标?

Thanks, 谢谢,

Eric 埃里克

Unfortunately, either all or none beans use CGLIB and if you use proxying of target class in one place, it is forced in all other places. 不幸的是,全部或没有bean都使用CGLIB,如果你在一个地方使用目标类的代理,它会被强制在所有其他地方。 Quoting 8.6 Proxying mechanisms of official documentation: 引用8.6官方文件的代理机制

Note 注意

Multiple <aop:config/> sections are collapsed into a single unified auto-proxy creator at runtime, which applies the strongest proxy settings that any of the <aop:config/> sections (typically from different XML bean definition files) specified. 多个<aop:config/>部分在运行时折叠为单个统一的自动代理创建器,它应用指定的任何<aop:config/>部分(通常来自不同的XML bean定义文件)的最强代理设置。 This also applies to the <tx:annotation-driven/> and <aop:aspectj-autoproxy/> elements. 这也适用于<tx:annotation-driven/><aop:aspectj-autoproxy/>元素。

To be clear: using 'proxy-target-class="true"' on <tx:annotation-driven/> , <aop:aspectj-autoproxy/> or <aop:config/> elements will force the use of CGLIB proxies for all three of them . 需要明确的是:在使用'proxy-target-class="true"'<tx:annotation-driven/> <aop:aspectj-autoproxy/><aop:config/>元素将强制使用CGLIB代理为他们三个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM