9

我有一个 Spring Web 应用程序,它被配置为使用 JDK 代理进行 AOP。AOP 注释(例如@Transactional)是在接口上声明的,而不是在实现类上声明的。

应用程序本身运行良好,但是当我运行单元测试时,它似乎试图将 CGLIB 用于 AOP 功能(而不是 JDK 代理)。这会导致测试失败 - 我在下面附加了堆栈跟踪。

我不明白为什么在运行测试时使用 CGLIB,因为 Spring 配置与应用程序运行时的配置基本相同。一个可能显着的区别是测试配置使用DataSourceTransactionManager而不是 JTA 事务管理器。测试类本身都扩展AbstractJUnit4SpringContextTests,难道这个类以某种方式硬连线以使用 CGLIB?

Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class $Proxy25]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class $Proxy25
    at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:213)
    at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:110)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:488)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:363)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:324)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:361)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1343)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
    ... 79 more
Caused by: java.lang.IllegalArgumentException: Cannot subclass final class class $Proxy25
    at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:446)
    at net.sf.cglib.transform.TransformingClassGenerator.generateClass(TransformingClassGenerator.java:33)
    at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
    at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
    at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:285)
    at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:201)
    ... 86 more

编辑:其中一位评论员要求我发布 Spring configuration。我以缩写形式将它包含在下面(即省略了不相关的 bean 和 XML 名称空间)。

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans>                 
    <!-- ANNOTATION SUPPORT -->
    <!-- Include basic annotation support -->
    <context:annotation-config/>        

    <!-- CONTROLLERS -->
    <!-- Controllers, force scanning -->
    <context:component-scan base-package="com.onebigplanet.web.controller,com.onebigplanet.web.ws.*"/>  

    <!-- Post-processor for @Aspect annotated beans, which converts them into AOP advice -->
    <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator">
        <property name="proxyTargetClass" value="true"/>
    </bean>

    <!-- An @Aspect bean that converts exceptions thrown in POJO service implementation classes to runtime exceptions  -->
    <bean id="permissionAdvisor" class="com.onebigplanet.web.advisor.PermissionAdvisor"/>
    <bean id="businessIntelligenceAdvisor" class="com.onebigplanet.web.advisor.bi.BusinessIntelligenceAdvisor"/>        

    <!-- Finds the controllers and sets an interceptor on each one -->
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <list>
                <bean class="com.onebigplanet.web.interceptor.PortalInterceptor"/>              
            </list>
        </property>
    </bean> 

    <!-- METHOD HANDLER ADAPTER --> 
    <!-- Finds mapping of url through annotation on methods of Controller -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="cacheSeconds" value="0"/>
        <property name="webBindingInitializer">
            <bean class="com.onebigplanet.web.binder.WebBindingInitializer"/>
        </property>
    </bean> 
</beans>

applicationContext-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans> 
    <!-- Declares a bunch of bean post-processors -->
    <context:annotation-config/>

    <context:component-scan base-package="com.onebigplanet.service.impl,com.onebigplanet.dao.impl.mysql" annotation-config="false"/>    

    <!-- Property configurer -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="/WEB-INF/obp-service.properties" />
    </bean> 

    <!-- Post-processor for @Aspect annotated beans, which converts them into AOP advice -->
    <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"/>

    <!-- An @Aspect bean that converts exceptions thrown in service implementation classes to runtime exceptions  -->
    <bean id="exceptionAdvisor" class="com.onebigplanet.service.advisor.ExceptionAdvisor"/>
    <bean id="cachingAdvisor" class="com.onebigplanet.service.advisor.CacheAdvisor"/>   
    <bean id="businessIntelligenceAffiliateAdvisor" class="com.onebigplanet.service.advisor.BusinessIntelligenceAffiliateAdvisor"/>

    <!-- Writable datasource -->
    <jee:jndi-lookup id="dataSource" jndi-name="java:/ObpDS"/>

    <!-- ReadOnly datasource -->
    <jee:jndi-lookup id="readOnlyDataSource" jndi-name="java:/ObpReadOnlyDS"/>  

    <!-- Map the transaction manager to allow easy lookup of a UserTransaction -->
    <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>

    <!-- Annotation driven transaction management -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

applicationContext-test.xml这仅在运行单元测试时包含。它的目的是覆盖在其他配置文件中声明的一些 bean。

<?xml version="1.0" encoding="UTF-8"?>
<beans>         
    <!-- Overwrite the property configurer bean such that it reads the test properties file instead -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="/obp-test.properties"/>
    </bean> 

    <!-- All DAOs should use the test datasource -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${testDataSource.driverClassName}"/>
        <property name="url" value="${testDataSource.url}"/>
        <property name="username" value="${testDataSource.username}"/>
        <property name="password" value="${testDataSource.password}"/>
    </bean>

    <bean id="readOnlyDataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${testDataSource.driverClassName}"/>
        <property name="url" value="${testDataSource.url}"/>
        <property name="username" value="${testDataSource.username}"/>
        <property name="password" value="${testDataSource.password}"/>
    </bean>

    <!-- 
        Overwrite the JTA transaction manager bean defined in applicationContent-service.xml with this one because
        the implementation of the former is provided by JBoss
    -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
<beans>
4

3 回答 3

8

听起来您引用的是实现类而不是接口。这里有一段更详细的摘录。

Spring 论坛帖子:“混合 JDK 和 CGLIB 代理”

一篇很棒的博文,解释了 JDK 与 CGLIB 代理的优缺点

于 2008-12-05T21:05:39.043 回答
3

嘿,Jean,CGLib 代理是通过子类化要代理的类来创建的——您正在尝试代理另一个不允许的代理,因为代理本身就是最终类。因此:

Caused by: java.lang.IllegalArgumentException: Cannot subclass final class class $Proxy25

于 2008-12-06T05:54:27.290 回答
2

I don't know if the solution was already shared and I am also sure the original requester must have found a solution, since it is a one year old query. For public interest however let me mention it here. Spring was using CGLIB because of the following declaration.

<!-- Post-processor for @Aspect annotated beans, which converts them into AOP advice --> 
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator">
      <property name="proxyTargetClass" value="true"/>
</bean>

The property should be set to false, so that the CGLIB is not triggered instead JDK Dynamic Proxying.

<property name="proxyTargetClass" value="false"/>

Hope that helps.

于 2010-01-27T01:19:46.510 回答