1. 错误
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'roleServiceImpl' is expected to be of type 'cn.wanghao.springSecurity.service.impl.RoleServiceImpl' but was actually of type 'com.sun.proxy.$Proxy22'
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:392)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1111)
at cn.wanghao.springSecurity.test.TestSpring.test(TestSpring.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
2. 错误原因
spring对AOP有两种实现方式:
- 一种是JDK的动态代理。它是基于接口方式实现的,要求所代理的类是一个接口类或者继承了接口,对一般的类就无法代理,spring默认是这种;
例如mybatis实现dao层的代理就是直接代理的接口。 - 一种是CGLIB动态代理。通过设置proxy-target-class=“true”,可以启动CGLIBD的动态代理,CGLIB直接生成二进制码,使得普通类也可以实现AOP。
另外,如果同时对一个类使用动态代理和IOC创建对象,结果是只会实现动态代理而IOC不创建其对象。
mybatis整合到spring中,则创建的代理对象也会交给IOC,且代理对象的名字和IOC实例化对象名字的规范一样,也是类名首字母小写
如果是JDK动态代理,则生成的代理对象的类型是其接口类型;如果是cglib动态代理,则是该类的类型。
RoleService是service层的接口,RoleServiceImpl是其实现类,具体代码如下:
@Service
public class RoleServiceImpl implements RoleService {
@Autowired
private RoleDao roleDao;
@Override
public SysRole selectByID(Integer id) {
System.out.println("RoleServiceImpl...");
return roleDao.selectByID(id);
}
}
spring配置文件中的aop配置如下:
<!-- 3. 配置 aop -->
<aop:config>
<!-- 配置切入点表达式 -->
<aop:pointcut expression="execution(* cn.wanghao.springSecurity.service.impl.*.*(..))" id="pointcut1"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
</aop:config>
运行代码如下:
@Test
public void test() {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
RoleServiceImpl roleServiceImpl = ac.getBean("roleServiceImpl", RoleServiceImpl.class);
SysRole sysRole = roleServiceImpl.selectByID(1);
System.out.println(sysRole.toString());
}
错误显示表明IOC容器中没有roleServiceImpl'
,但是有RoleServiceImpl'
这个类的代理对象。而我使用的是spring默认的JDK动态代理,再结合上面的知识,你应该明白了,肯定是自己在哪里设置了RoleServiceImpl'
的动态代理,但是你自己不知道。
通过排查,我发现我只有在AOP配置中使用了动态代理,结果发现果然对其设置了动态代理:execution(* cn.wanghao.springSecurity.service.impl.*.*(..))
。而且使用的是JDK动态代理,而JDK动态代理最终该代理类是如下形式:
public class $Proxy22 extends Proxy implements RoleService{
...
}
动态生成的代理类的类型是被代理对象的接口类型,所以容器中的代理对象应该是名字为roleServiceImpl
,但是类型为RoleService
3. 解决方法
3.1 第一种方法
既然,同时对一个类使用动态代理和IOC创建对象,结果是只会实现动态代理而IOC不创建其对象,那么我们可以选择使用·cglib·动态代理创建RoleServiceImpl
类型的roleServiceImpl
。
所以,直接在AOP配置的后面加上<aop:aspectj-autoproxy proxy-target-class="true"/>
这一行代码:
<!-- 3. 配置 aop -->
<aop:config>
<!-- 配置切入点表达式 -->
<aop:pointcut expression="execution(* cn.wanghao.springSecurity.service.impl.*.*(..))" id="pointcut1"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
</aop:config>
<aop:aspectj-autoproxy proxy-target-class="true"/>
3.2 第二种方法
按照我们刚才分析的,去使用该对象。
@Test
public void test() {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
RoleServiceImpl roleServiceImpl = ac.getBean("roleServiceImpl", RoleService.class);
SysRole sysRole = roleServiceImpl.selectByID(1);
System.out.println(sysRole.toString());
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/84647.html