Spring AOP的简单示例
配置文件
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 激活组件扫描功能,在包cn.ysh.studio.spring.aop及其子包下面自动扫描通过注解配置的组件 --> <context:component-scan base-package="com.zhiguoguo.service,aspect"/> <!-- 激活自动代理功能 --> <aop:aspectj-autoproxy proxy-target-class="true"/> </beans>
service类:
package com.zhiguoguo.service; import org.springframework.stereotype.Component; @Component public class HelloService { public String sayHello() { System.out.println("——————方法执行——————"); // throw new RuntimeException("haha"); return "Hello world!"; } }
切面AOP:
package com.zhiguoguo.aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; /** * 定义切面 */ @Component @Aspect public class HelloServiceAspect { @Before("execution(* com.zhiguoguo.service.HelloService.*(..))") public void authorith(){ System.out.println("模拟进行权限检查。"); } @After("execution(* com.zhiguoguo.service.HelloService.*(..))") public void release() { System.out.println("模拟方法结束后的释放资源..."); System.out.println(); } @AfterReturning(returning="obj", pointcut="execution(* com.zhiguoguo.service.HelloService.*(..))") public void log(Object obj) { System.out.println("模拟目标方法返回值:" + obj); System.out.println("模拟记录日志功能..."); System.out.println(); } @AfterThrowing(throwing="ex", pointcut="execution(* com.zhiguoguo.service.HelloService.*(..))") public void doRecoverActions(Throwable ex) { System.out.println("目标方法中抛出的异常:" + ex); System.out.println("模拟抛出异常后的增强处理..."); } // @Around("execution(* com.zhiguoguo.service.HelloService.*(..))")//这里先注释掉 public Object processTx(ProceedingJoinPoint jp) throws java.lang.Throwable { System.out.println("执行目标方法之前,模拟开始事物..."); // 执行目标方法,并保存目标方法执行后的返回值 Object rvt = jp.proceed(); //这里的切面方法如果有参数才能传递一个参数给他 // Object rvt = jp.proceed(new String[]{"被改变的参数"}); System.out.println("执行目标方法之前,模拟结束事物..."); System.out.println(); //经过这么一个环绕,可以增强返回值 return rvt + "新增的内容"; } }
主函数:
package main; import com.zhiguoguo.service.HelloService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); HelloService helloService = context.getBean(HelloService.class); helloService.sayHello(); } }
本文出自 无忧之路 - 博客园
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2013-11-26 Hibernate 缓存 关于注解方式