Spring AOP错误Bean named is expected to be of type'xx'but was actually of type 'com.sun.proxy.$Proxy5'

Spring AOP问题:

使用.class加载报错,使用bean的name正常运行
在测试代码中,使用class直接获取bean会出现如下错误:
使用到.class获取bean,出现以下错误。
使用bean的name获取不会出现错误。

Exception in thread "main" org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'studentDaoImpl' is expected to be of type 'com.xsl.mapper.StudentDaoImpl' ****but was actually of type 'com.sun.proxy.$Proxy5'****
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:384)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1086)
	at com.xsl.test.TestTransaction.main(TestTransaction.java:10)

代码如下:

 public static void transactionXML() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring_tx.xml");
        StudentDaoImpl studentDao = applicationContext.getBean(StudentDaoImpl.class);//getBean("studentDaoImpl",StudentDaoImpl.class)也会报错
        studentDao.testAno();
    }

XML配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
    <bean name="studentDaoImpl" class="com.xsl.mapper.StudentDaoImpl">
        <property name="jdbcTemplate" ref="template"></property>
    </bean>
    <bean name="template" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dbSource"></property>
    </bean>
    <bean name="dbSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/xxx?serverTimezone=GMT%2B8&amp;useSSL=false"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <!-- 定义事务管理器 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dbSource"/>
    </bean>
    <!--配置事务的propagation传播性(传播行为)-->
    <tx:advice transaction-manager="txManager" id="txAdvice">
        <tx:attributes>
            <tx:method name="*" isolation="READ_COMMITTED" propagation="REQUIRED"/>
            <tx:method name="get*" propagation="NOT_SUPPORTED" read-only="true"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="myPointCut" expression="execution(* com.xsl.mapper.StudentDaoImpl.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointCut"></aop:advisor>
    </aop:config>
</beans>

问题解决:
在aop:config标签中加入 proxy-target-class=“true”

<aop:config proxy-target-class="true">
       ......
</aop:config>

注:proxy-target-class—>将代理模式设置为由CGLIB进行代理。
InvocationHandler实现动态代理:如果被代理的类未实现任何接口,那么不能采用通过InvocationHandler动态代理的方式去代理它的行为。
CGLIB实现动态代理:通过CGLIB成功创建的动态代理,实际是被代理类的一个子类。那么如果被代理类被标记成final,也就无法通过CGLIB去创建动态代理。

至此,问题解决。
问题分析:
Spring在进行动态代理的时候,会根据得到的类,进行自动设置使用哪种动态代理的实现方法。
1.创建的类没有实现接口时:默认使用CGLIB进行动态代理的实现。
2.创建的类使用到接口时:默认使用InvocationHandler进行动态代理的实现。
使用接口时,Spring将使用InvocationHandler进行动态代理,而使用InvocationHandler的时候,代理类会实现与被代理类的相同接口,在调用applicationContext.getBean(XX.class);的时候,得到的是代理对象,而代理对象与被代理对象属于同层(实现一个接口,类似继承一个父类)不能像上/下转型,因此报错。
而使用bean的name进行获取的时候,指向的是该bean所对应的对象,因此可以转换,不会出现报错。

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值