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报错的更多相关文章

  1. 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 ...

  2. org.springframework.beans.factory.BeanNotOfRequiredTypeException

    写一个代码:关于Spring Bean的装配.基于annotation实现的范例代码. 出现了错误: 十一月 14, 2018 4:51:01 下午 org.springframework.conte ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException

    You should autowire interface AbstractManager instead of class MailManager. If you have different im ...

  7. 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 ...

  8. 开发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 ...

  9. 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问题.解决后在此记录一下. 解 ...

随机推荐

  1. [loj3304]作业题

    (以下假设$T=(V,\{e_{1},e_{2},...,e_{n-1} \})$是一棵树) 根据莫比乌斯反演,有$\gcd(w_{1},w_{2},...,w_{e_{n-1}})=\sum_{d| ...

  2. 【JavaSE】JDK配置

    Java开发环境配置 2020-09-10  08:32:20  by冲冲 1. Windows7安装JDK 1.1 下载JDK ① 下载地址:http://www.oracle.com/techne ...

  3. html+css第七篇-表格

    表格标签: table 表格 thead 表格头 tbody 表格主体 tfoot 表格尾 tr 表格行 th 元素定义表头 td 元素定义表格单元 表格样式重置 table{border-colla ...

  4. appdata 文件夹

    appdata file AppData 的位置在 c:\Users\{UserName}\Appdata ,它是从 Windows Vista 开始引入的,直至今天的 Windows 7, 8, 1 ...

  5. Codeforces 1396D - Rainbow Rectangles(扫描线+线段树)

    Codeforces 题面传送门 & 洛谷题面传送门 一道鸽了整整一年的题目,上一次提交好像是 2020 年 9 月 13 日来着的(?) 乍一看以为第 2 个提交和第 3 个提交只差了 43 ...

  6. [R]在dplyr函数的基础上编写函数-(3)tidyeval

    dplyr的优点很明显,数据框操作简洁,如filter(df, x == 1, y == 2, z == 3)等于df[df$x == 1 & df$y ==2 & df$z == 3 ...

  7. [Linux] 非root安装GCC9.1.0

    说明 一般Linux系统自带或公共的GCC版本都很低,如目前我们的服务器版本的GCC还停留在gcc-4.9.3,而官网已到达9.2版本(下载http://ftp.gnu.org/gnu/gcc/) , ...

  8. R包 tidyverse 分列

    代码: 1 library(tidyverse) 2 separate(data = df,col=chr_pos,into=c("chr","pos"),se ...

  9. Golang gRPC调试工具

    目录 Golang gRPC调试工具 1. 命令行工具 grpcurl 1.1 安装 1.2 验证 1.3 注册反射 1.4 使用示例 2. web调试工具grpcui 2.1 安装 2.2 验证 2 ...

  10. BeautifulSoup解析库的介绍和使用

    ### BeautifulSoup解析库的介绍和使用 ### 三大选择器:节点选择器,方法选择器,CSS选择器 ### 使用建议:方法选择器 > CSS选择器 > 节点选择器 ## 测试文 ...