admin管理员组文章数量:1566604
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/schema/beans"
xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework/schema/aop"
xmlns:context="http://www.springframework/schema/context"
xmlns:tx="http://www.springframework/schema/tx"
xsi:schemaLocation="http://www.springframework/schema/beans
http://www.springframework/schema/beans/spring-beans.xsd
http://www.springframework/schema/context
http://www.springframework/schema/context/spring-context.xsd
http://www.springframework/schema/aop
http://www.springframework/schema/aop/spring-aop.xsd
http://www.springframework/schema/tx
http://www.springframework/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&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所对应的对象,因此可以转换,不会出现报错。
版权声明:本文标题:Spring AOP错误Bean named is expected to be of type'xx'but was actually of type 'com.sun.proxy.$Proxy5' 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1726602544a1077203.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论