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

Eri*_* B. 5 java aop spring cglib proxies

这个线程与我在这里遇到的有关访问受建议类的受保护方法的需求有关.我正在使用Spring 3.0.6,并创建了一个Spring分析方面,我正在使用JDK代理应用于大量的bean.

但是,由于需要访问一个特定bean中的受保护方法,我想使用CGLIB建议它.所有其他bean我想继续使用JDK Proxies.

我使用了注释和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>
Run Code Online (Sandbox Code Playgroud)

我试图将配置拆分为两个,并且对于一个配置指定target-class="true"和另一个target-class="false",但它似乎在那时将CGLIB应用于所有.

有没有办法实现这个目标?

谢谢,

埃里克

Tom*_*icz 7

不幸的是,全部或没有bean都使用CGLIB,如果你在一个地方使用目标类的代理,它会在所有其他地方被强制使用.引用8.6官方文件的代理机制:

注意

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

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

  • 谢谢回复。我以前很怕那个。但后来我发现[这篇文章](http://stackoverflow.com/q/7638251/827480)这似乎表明他得到了它的工作,但即使我用`@Scope( proxyMode 注释我的目标类(JAXBPoolImpl) = ScopedProxyMode.TARGET_CLASS )` 它似乎对我不起作用。 (2认同)