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

那年仲夏 提交于 2019-12-09 06:22:32

问题


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.

However, due to the need to access protected methods in one particular bean, I would like to advise it using CGLIB. All other beans I would like to continue to use JDK Proxies.

I am using a mix of annotations and xml configuration, but this particular aspect is defined in XML configuration.

I know that there is <aop:scoped-proxy> tag, but from what I can tell, that applies to all aspects.

Is there anyway to define for a single aspect to use CGLIB instead?

<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.

Is there any way to accomplish this?

Thanks,

Eric


回答1:


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. Quoting 8.6 Proxying mechanisms of official documentation:

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. This also applies to the <tx:annotation-driven/> and <aop:aspectj-autoproxy/> elements.

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.



来源:https://stackoverflow.com/questions/10110569/how-to-mix-cglib-and-jdk-proxies-in-spring-configuration-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!