spring_AOP的注解开发
2024-09-06 05:23:32
logger日志类:
package cn.mepu.utils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; /** * 记录日志,提供公共代码 * @author shkstart * @create 2019-11-09 15:25 */ @Component("logger") @Aspect //表示当前类是一个切面类 public class logger { //表达式 @Pointcut("execution(* cn.mepu.service.imp.*.*(..))") private void pt1(){ } /** * 前置日志 */ @Before("pt1()") public void beforePrintLog(){ System.out.println(" logger中前置开始记录日志" ); } /** * 后置日志 */ @AfterReturning("pt1()") public void afterreturningPrintLog(){ System.out.println(" logger中后置开始记录日志" ); } /** * 异常日志 */ @AfterThrowing("pt1()") public void afterThrowingPrintLog(){ System.out.println(" logger中异常开始记录日志" ); } /** * 最终日志 */ @After("pt1()") public void afterPrintLog(){ System.out.println(" logger中最终开始记录日志" ); } //@Around("pt1()") public Object aroundPringLog(ProceedingJoinPoint pjp){ Object reValue = null; try { Object[] args = pjp.getArgs();//得到方法所需的参数 //前置通知 System.out.println("logger中环绕通知开始记录日志前置" ); pjp.proceed(args);//明确调用切入点方法 //后置通知 System.out.println("logger中环绕通知开始记录日志后置" ); return reValue; } catch (Throwable throwable) { //异常通知 System.out.println("logger中环绕通知开始记录日志异常" ); throw new RuntimeException(throwable); }finally { //最终通知 System.out.println("logger中环绕通知开始记录日志最终" ); } } }
service层:
package cn.mepu.service.imp; import cn.mepu.service.IAccountService; import org.springframework.stereotype.Service; /** * 账户业务层实现类 * @author shkstart * @create 2019-11-09 15:23 */ @Service("accountService") public class AccountServiceImp implements IAccountService { @Override public void saveAccount() { System.out.println("保存"); } @Override public void updateAccount(int i) { System.out.println("更新"+i); } @Override public int deleteAccount() { System.out.println("删除"); return 0; } }
测试类:
package cn.mepu.Test; import cn.mepu.service.IAccountService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 测试Aop * @author shkstart * @create 2019-11-09 15:59 */ public class AopTest { public static void main(String[] args) { //1.获取容器 ApplicationContext ac = new ClassPathXmlApplicationContext("Bean.xml"); //2.获取对象 IAccountService as = (IAccountService) ac.getBean("accountService"); //3.执行方法 as.saveAccount(); } }
Bea文件:
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置spring创建容器时要扫描的包--> <context:component-scan base-package="cn.mepu"></context:component-scan> <!-- 配置spring开启注解AOP的支持 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
POM文件;
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.mepu</groupId> <artifactId>day05_spring_aop_annotation</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version> </dependency> </dependencies> </project>
最新文章
- [转]ASP.NET Core 之 Identity 入门(二)
- Fedora 23 忘记root密码
- nodejs初探(二)第一个nodejs程序“hello world”
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-006- 如何保持重定向的request数据(用model、占位符、RedirectAttributes、model.addFlashAttribute(";spitter";, spitter);)
- CSharp tar类型文件压缩与解压
- 打包jar类库与使用jar类库
- AndroidStudio引入so文件
- LightOJ 1234 Harmonic Number 调和级数部分和
- Timestamp解析0000-00-00 00:00:00报格式错误
- Unity3d&;C#分布式游戏服务器ET框架介绍-组件式设计
- Node入门教程(4)第三章:第一个 Nodejs 程序
- Python函数之匿名函数
- Jenkins创建job时Check-out Strategy各个选项详细说明(含图)
- [CNN] Understanding Convolution
- ios开发之--调整UISearchBar的输入框的背景颜色
- sklearn 中的 Pipeline 机制 和FeatureUnion
- Spring 中出现相同名称的 bean 的处理机制
- MySQL学习笔记:少用Null
- SQL Server中将多行数据拼接为一行数据并且有特殊字符
- 框架----Django之Ajax全套实例(原生AJAX,jQuery Ajax,“伪”AJAX,JSONP,CORS)