Spring5 AOP编程:关于org.springframework.beans.factory.BeanNotOfRequiredTypeException报错
Spring5 AOP编程:关于org.springframework.beans.factory.BeanNotOfRequiredTypeException报错
先上错误详细信息:
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'bookDaoImpl' is expected to be of type 'com.atguigu.dao.BookDaoImpl' but was actually of type 'com.sun.proxy.$Proxy19'
直译过来大概意思是说:这个叫做bookDaoImpl的Bean,应该是BookDaoImpl类型,然而现在是com.sun.proxy.$Proxy19类型
疑惑????查看对应源码:下图第七行报错!
@Test
public void addTest() { // 在数据库中,添加 现代光电测试
// 1. Spring读取配置文件
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
// 2. 获取BookDaoImpl对象 (错误行)
BookDaoImpl bookDaoImpl = context.getBean("bookDaoImpl", BookDaoImpl.class);
// 3. 测试方法
bookDaoImpl.add(new Book(18, "现代光电测试", 120, 4));
}
从这段代码上,bookDaoImpl确实应该是BookDaoImpl类型,按理说代码没写错,为啥getBean()方法返回的不是BookDaoImpl类型。直觉告诉我,应该是增强类代码出现问题,上增强类代码:
@Service // 创建对象
@Aspect // 这是一个代理对象
public class BookProxy { // 增强类
@Before(value = "execution(* com.atguigu.dao.BookDaoImpl.add(..))") // 注意参数 .. 别忘了
@Order(1)
public void addBefore() {
System.out.println("Proxy01:添加方法准备就绪!");
}
@Before(value = "execution(* com.atguigu.dao.BookDaoImpl.add(..))")
@Order(2)
public void addBefore01() {
System.out.println("Proxy02:执行!");
}
}
检查代码之后,发现增强类也没问题啊............
没想明白
最后求助百度,发现有人说这个错误可能是因为JDK动态代理不支持类注入导致的。
检查被增强类代码,发现确实如此,一切的源头:下图第六行代码,JdbcTemplate已经是一个类对象,使用JDK动态代理无法进行注入类对象的操作。
原增强类代码:
@Service(value = "bookDaoImpl")
public class BookDaoImpl implements BookDao{
// 注入JdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate; // JDK动态代理并不能进行类的注入!!
.......
}
因为JDK动态代理只支持创建接口实现类代理对象,从而增强类的方法。**
于是,解决方案如下:
新建一个被增强类,命名为BookServiceProxy:其中BookDao是接口,也就是原被增强类BookDaoImpl实现的接口。
解决方案的核心思想:通过一个类封装接口对象,并在这个类中的各个方法中,调用这个接口对应的方法,然后通过增强这个类的各个方法,从而达到 对 实现这个接口的类 的方法 的增强(有点绕,相当于套娃)。
至于为什么这么做?是因为JDK动态代理的原理,就是通过创建接口的另一个实现类,被将其作为代理对象,然后在类代理对象中,编写增强逻辑,从而对原实现类的方法进行增强。
@Service(value = "bookServiceProxy")
public class BookServiceProxy {
// 注入BookDao
@Autowired
private BookDao bookDao; // 被增强类所实现的BookDao接口,因为是接口类型,能够进行注入!
// add
public void addBook(Book book) { // Book 为 entity
bookDao.add(book);
}
// update
public void updateBook(int classhours, String name) {
bookDao.update(classhours, name);
}
// delete
public void deleteBook(String name) {
bookDao.delete(name);
}
}
重写编写增强类代码,对新的被增强类BookServiceProxy进行切面操作。
@Service // 创建对象
@Aspect // 这是一个代理对象
public class BookProxy { // 增强类
@Before(value = "execution(* com.atguigu.service.BookServiceProxy.addBook(..))") // 注意参数 .. 别忘了
@Order(1)
public void addBefore() {
System.out.println("Proxy01:添加方法准备就绪!");
}
@Before(value = "execution(* com.atguigu.service.BookServiceProxy.addBook(..))")
@Order(2)
public void addBefore01() {
System.out.println("Proxy02:执行!");
}
}
测试代码:
@Test
public void BookServiceProxyTest() {
// 1. Spring读取配置文件
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
// 2. 获取BookDaoImpl对象
BookServiceProxy bookServiceProxy = context.getBean("bookServiceProxy", BookServiceProxy.class);
// 3. 测试方法
bookServiceProxy.addBook(new Book(18, "现代光电测试", 120, 4));
}
运行结果:
Proxy01:添加方法准备就绪!
Proxy02:执行!
10月 15, 2021 9:50:01 下午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
信息: {dataSource-1} inited
插入操作执行成功!Process finished with exit code 0
Spring5 AOP编程:关于org.springframework.beans.factory.BeanNotOfRequiredTypeException报错的更多相关文章
- Spring错误之org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'bookService' is expected to be of type 'pw.fengya.tx.BookService' but was actually of type 'com.sun.proxy.$Proxy1
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cas ...
- org.springframework.beans.factory.BeanNotOfRequiredTypeException
写一个代码:关于Spring Bean的装配.基于annotation实现的范例代码. 出现了错误: 十一月 14, 2018 4:51:01 下午 org.springframework.conte ...
- Spring AOP 报错org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'XXXXXX' defined in class path resource..........
完整报错如下: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'befo ...
- Spring AOP:Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException
1 报错 Exception encountered during context initialization - cancelling refresh attempt: org.springfra ...
- Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException:
严重: Exception sending context initialized event to listener instance of class org.springframework.we ...
- nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException
You should autowire interface AbstractManager instead of class MailManager. If you have different im ...
- ssh整合报错严重: Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxx'
错误描述:eclipse整合ssh的时候 报不能创建名字为xxx的对象 信息: Destroying singletons in org.springframework.beans.factory.s ...
- 开发Spring过程中几个常见异常(二):Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'a' define
本异常是小编在运行自己另外一篇博文中的例子时遇到的.(附博文:http://www.cnblogs.com/dudududu/p/8482487.html) 完整异常信息: 警告: Exception ...
- Caused by:org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type "" available: expected at least 1 bean which qualifies as autowire candidate
项目使用spring, mybatis.因为分了多个模块,所以会这个模块引用了其它模块的现在,结果使用Junit测试的时候发现有两个模块不能自动注入dao和service问题.解决后在此记录一下. 解 ...
随机推荐
- [nowcoder5671D]Data structure
问题相当于统计$且\sum_{l\le x<y\le r且lca(x,y)=x}1=c(sz[x],2)-\sum_{son}c(sz[son],2)$,考虑用莫队来维护区间,那么相当于要支持: ...
- js offset系列属性
offsetParent:返回该元素有定位的父级,如果父级都没有定位则返回body offsetTop:返回元素相对父级(带有定位的父级)上方的偏移 offsetLeft:返回元素相对父级(带有定位的 ...
- CF1004D Sonya and Matrix
不要想当然. 考虑到我们一定有存在个数为\(4\)的倍数的数. 否则第一个不是的数即为\(x\). 那么我们设\(b\)为所有的数的最大值. 那么显然有\(|n - x| + |m - y| = b\ ...
- Codeforces 407E - k-d-sequence(单调栈+扫描线+线段树)
Codeforces 题面传送门 & 洛谷题面传送门 深感自己线段树学得不扎实-- 首先特判掉 \(d=0\) 的情况,显然这种情况下满足条件的区间 \([l,r]\) 中的数必须相同,双针扫 ...
- NOI 2008 志愿者招募
NOI 2008 志愿者招募 考虑用 $ p_i $ 表示第 $ i $ 天实际招收的人数,我们假设我们有三种志愿者,分别是 $ 1\to 2,1 \to 3 , 2\to 3 $ ,我们招手的人数分 ...
- Codeforces 739C - Alyona and towers(线段树)
Codeforces 题目传送门 & 洛谷题目传送门 可能有人会问我为什么为这道 *2500 的 D1C 写题解,我觉得大概是想要在写题解数量上 dd ycx 吧,因为 ycx 到目前为止写了 ...
- mount 挂载详解
挂接命令(mount) 首先,介绍一下挂接(mount)命令的使用方法,mount命令参数非常多,这里主要讲一下今天我们要用到的. 命令格式:mount [-t vfstype] [-o option ...
- FASTA/Q序列处理神器---seqkit
该软件对于处理FASTA/Q十分方便,省去自己编写脚本 安装 1 conda install seqkit 使用 序列操作(seq) 1 ## 取方向序列 2 seqkit seq test.fa - ...
- eggNOG 5.0数据库介绍
目录 1. eggNOG简介 2. eggNOG-Mapper注释原理 3. eggNOG 5.0数据资源 4. eggNOG-Mapper使用 5. NOG.KOG.COG.KEGG.GO区别? 1 ...
- 毕业设计之ansible_quan_bbs设置
ansible创建连接: 客户端(管理节点) 可能需要安装包: yum install -y libselinux-python 实现ssh免密码登陆管理的服务器 [apps@anza ~]$ sud ...