Spring 通过配置注解实现 AOP

news/2025/3/11 7:33:53/

在编写代码的过程中出现了这样的错误:

Exception in thread "main" org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'studentServiceImpl' is expected to be of type 'com.service.impl.StudentServiceImpl' but was actually of type 'jdk.proxy2.$Proxy14'at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:384)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:205)at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1091)at com.test.TestDemo.main(TestDemo.java:14)

问了一下豆包,

  1. AOP 代理机制:Spring AOP 默认使用 JDK 动态代理(当目标对象实现了接口时),JDK 动态代理会创建一个实现了目标对象接口的代理类,而不是目标对象的实际类。因此,当你尝试直接使用目标对象的实现类类型来获取 Bean 时,就会抛出类型不匹配的异常。
  2. 类型获取错误:在代码中错误地使用了目标对象的实现类类型来获取 Bean,而应该使用其接口类型

Spring 中可以通过注解来实现AOP,通过注解来实现的具体步骤如下:

首先我们来看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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--定义扫描类  --><context:component-scan  base-package="com"></context:component-scan><aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

aop:aspectj-autoproxy 是启动相关的支持aop

定义切面类:

java">@Aspect
@Component
public class LogAspect {@Before("execution(* com.service.impl.*.*(..))")public void beforePrintLog() {System.out.println("LogAspectbeforePrintLog");}public void afterReturnPrintLog() {System.out.println("LogAspectafterReturnPrintLog");}public void afterThrowingPrintLog() {System.out.println("LogAspectafterThrowingPrintLog");}public void afterPrintLog() {System.out.println("LogAspectafterPrintLog");}public void aroundPrintLog() {System.out.println("aroundPrintLog");}
}

 切面类的注释除了@Aspect 之外,Spring 该有的注释也需要有 @Component

切点类:

java">package com.service.impl;import org.springframework.stereotype.Service;import com.service.StudentService;@Service
public class StudentServiceImpl  implements StudentService{@Overridepublic void study() {System.out.println("正在学习...............");}}

注意的是:切点需要实现某个接口

测试类:

java">public class TestDemo {public static void main(String[] args) {ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");StudentServiceImpl ss=context.getBean("studentServiceImpl", StudentServiceImpl.class);ss.study();}}

 但是会出现开头的错误,我们需要用到接口的回调,也就是用接口来创建目标类,修改为如下:

java">	public static void main(String[] args) {ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");StudentService ss=context.getBean("studentServiceImpl", StudentService.class);ss.study();}

上述就是通过Spring注解来实现SpringAOP的功能

希望对你有所帮助


http://www.ppmy.cn/news/1578284.html

相关文章

ASP.NET CORE MVC EF框架

1.一个视图中的多个表单Form中的变量。 方式一&#xff1a;视图中跨Form变量不能用&#xff0c;得各自定义变量否则编译不能通过。变量名还不能相同。 或者方式二&#xff1a;在Form之外定义变量 {ViewData["Title"] "ExpenseForm"; } &#xfeff; {L…

图解JVM - 19.JVM监控及诊断工具-命令行篇

1. 概述 在JVM性能调优和故障排查中&#xff0c;命令行工具是开发运维人员最锋利的"手术刀"。如图1所示&#xff0c;这些工具可以分为三类&#xff1a; 核心工具家族&#xff1a; 进程定位&#xff1a;jps运行时监控&#xff1a;jstat参数管理&#xff1a;jinfo内存…

C++11新特性 13.共享智能指针shared_ptr

目录 一.基础介绍 1.基本概念 用途 2.语法 二.使用示例 示例1&#xff1a;基本使用 示例2&#xff1a;循环引用与解决方案 示例3&#xff1a;多线程安全示例 三.使用场景 1.对象需要在多个地方共享时 2. 在容器中存储指针时 3.解决异步编程中的生命周期问题 4.在…

两江产业集团董事长李克伟率团考察深兰科技,推动熊猫汽车与机器人板块落地重庆

2025年3月7日&#xff0c;重庆两江产业集团董事长李克伟率团来到深兰科技集团上海总部考察调研&#xff0c;并与深兰科技集团创始人、董事长陈海波等集团管理层座谈交流&#xff0c;双方围绕西南地区AI产业基地共建、自动驾驶汽车制造、智能机器人研发及产业协同等领域的合作展…

【C++基础二】缺省参数和函数重载

【C基础二】缺省参数和函数重载 1.缺省参数1.1全缺省1.2半缺省 2.什么是函数重载3.不同类型的函数重载4.为什么C支持函数重载而C语言不支持 1.缺省参数 缺省参数是声明或定义函数时&#xff0c;为函数的参数指定一个缺省值&#xff0c;在调用该函数时&#xff0c;若没有指定的实…

RAG助力机器人场景理解与具身操作!EmbodiedRAG:基于动态三维场景图检索的机器人任务规划

作者&#xff1a;Meghan Booker, Grayson Byrd, Bethany Kemp, Aurora Schmidt, Corban Rivera单位&#xff1a;约翰霍普金斯大学论文标题&#xff1a;EmbodiedRAG: Dynamic 3D Scene Graph Retrieval for Efficient and Scalable Robot Task Planning论文链接&#xff1a;http…

NFS,Nginx综合实验

1.实验要求 配置NFS服务器作为 ngxin 服务的存储目录&#xff0c;并在目录中创建 index.html 文件&#xff0c;当访问 http://你的IP 地址时&#xff0c;可以成功显示 index.html 文件的内容。 | 角色 | 软件 | IP | 主机名 | 系统 | | NFS服务器 | nfs-utils | 192.168.72.7…

音频进阶学习十九——逆系统(简单进行回声消除)

文章目录 前言一、可逆系统1.定义2.解卷积3.逆系统恢复原始信号过程4.逆系统与原系统的零极点关系 二、使用逆系统去除回声获取原信号的频谱原系统和逆系统幅频响应和相频响应使用逆系统恢复原始信号整体代码如下 总结 前言 在上一篇音频进阶学习十八——幅频响应相同系统、全…