SpringAOP配置要点
一、基于配置文件
1、关于aop配置文件相关
<!--配置aop-->
<aop:config>
<!--配置切入点表达式-->
<aop:pointcut id="aspect_method"
expression="execution(* service.Impl.AccountServiceImpl.save())"></aop:pointcut>
<!--配置aop切面-->
<aop:aspect id="logAdvice" ref="log">
<!--配置通知类型,并建立通知方法和切入点方法的关联-->
<!--配置前置通知 在切入点方法执行之前执行-->
<aop:before method="beforeAdvice" pointcut-ref="aspect_method"></aop:before>
<!--配置后置通知 在切入点方法执行之后且不发生异常执行-->
<aop:after-returning method="afterReturnAdvice" pointcut-ref="aspect_method"></aop:after-returning>
<!--配置异常通知 在切入点方法发生异常时执行,此时不会执行异常通知-->
<aop:after-throwing method="afterThrowsAdvice" pointcut-ref="aspect_method"></aop:after-throwing>
<!--配置最终通知 不管切入点方法是否发生异常都会执行-->
<aop:after method="afterAdvice" pointcut-ref="aspect_method"></aop:after> <!--配置环绕通知-->
<aop:around method="aroundLog" pointcut-ref="aspect_method"></aop:around>
</aop:aspect>
</aop:config>
2、切面类模板范例
package utils; import org.aspectj.lang.ProceedingJoinPoint; /**
* @Classname Logger
* @Description TODO
* @Date 2020/9/21 15:23
* @Created by Administrator
*/
public class Logger {
public void beforeAdvice() {
System.out.println("前置通知记录日志...");
} public void afterReturnAdvice() {
System.out.println("后置通知记录日志...");
} public void afterThrowsAdvice() {
System.out.println("异常通知记录日志...");
} public void afterAdvice() {
System.out.println("最终通知记录日志...");
} public Object aroundLog(ProceedingJoinPoint pjp){
Object rtValue=null;
try {
Object[] args = pjp.getArgs();
System.out.println("前置通知记录日志...");
rtValue = pjp.proceed(args);
System.out.println("后置通知记录日志...");
return rtValue;
} catch (Throwable throwable) {
System.out.println("异常通知记录日志...");
throwable.printStackTrace();
}finally {
System.out.println("最终通知记录日志...");
}
return rtValue;
}
}
3、切入点表达式写法
切入点表达式的写法:
1、标准写法
权限修饰符 返回值类型 包名.包名.包名..包名.类名.方法名(参数)
eg:public void service.Impl.AccountServiceImpl.save()
2、省略权限修饰符
返回值类型 包名.包名.包名..包名.类名.方法名(参数)
eg:void service.Impl.AccountServiceImpl.save()
3、返回值可使用通配符代替,表示任意类型
* 包名.包名.包名..包名.类名.方法名(参数)
eg:* service.Impl.AccountServiceImpl.save()
4、包名可使用通配符表示任意包名
1)* *.*.*.*.类名.方法名(参数)
2)* *..*.类名.方法名(参数)
5、参数列表
基本类型直接写名称 int
引用类型写包名.类名形式 java.lang.String
使用通配符*表示有参数
使用..表示有无参数均可
6、全通配
* *..*.*.*(..)
二、基于注解配置
1、配置类
package sun.config; import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy; /**
* @Classname SpringConfig
* @Description TODO
* @Date 2020/9/21 16:55
* @Created by Administrator
*/
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = "sun")
public class SpringConfig { }
2、切面类注解范例
package sun.utils; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component; /**
* @Classname Logger
* @Description TODO
* @Date 2020/9/21 15:23
* @Created by Administrator
*/
@Component("log")
@Aspect
public class Logger {
@Pointcut("execution(* sun.service.Impl.AccountServiceImpl.save())")
private void aspect_method() {
} // @Before("aspect_method()")
public void beforeAdvice() {
System.out.println("前置通知记录日志...");
} // @AfterReturning("aspect_method()")
public void afterReturnAdvice() {
System.out.println("后置通知记录日志...");
} // @AfterThrowing("aspect_method()")
public void afterThrowsAdvice() {
System.out.println("异常通知记录日志...");
} // @After("aspect_method()")
public void afterAdvice() {
System.out.println("最终通知记录日志...");
} @Around("aspect_method()")
public Object aroundLog(ProceedingJoinPoint pjp) {
Object rtValue = null;
try {
Object[] args = pjp.getArgs();
System.out.println("前置通知记录日志...");
rtValue = pjp.proceed(args);
System.out.println("后置通知记录日志...");
return rtValue;
} catch (Throwable throwable) {
System.out.println("异常通知记录日志...");
throwable.printStackTrace();
} finally {
System.out.println("最终通知记录日志...");
}
return rtValue;
}
}
SpringAOP配置要点的更多相关文章
- 自学Aruba3.2-Aruba配置架构-Virtual AP配置要点
点击返回:自学Aruba之路 自学Aruba3.2-Aruba配置架构-Virtual AP配置要点 1. AP.AP-Group和Virtual-AP的关系 解析列举: AP1.AP3, ...
- Hadoop配置要点必看
hadoop 配置要点 软件 版本 java 1.8.0_151 hadoop 2.6.1 part1 $HADOOP_HOME/etc/hadoop 目录下配置参数. slaves 配置从节点 ya ...
- H3C子接口配置要点及实例说明
类型一:以太网子接口配置要点(单臂路由) 第一步:在路由器对端的交换机上配置好vlan信息(如vlan10/vlan20) 第二步:将交换机上与路由器直接相连的以太口配置成trunk口并同意 ...
- ENSP静态链路聚合实验配置要点及实例
链路聚合分为:静态链路聚合.动态链路聚合链路聚合的负载分担模式:对于二层数据流,系统会根据MAC地址(源mac地址和目的mac地址来计算),而对于三层数据流则会根据ip地址来进行负载分担计算. 静态链 ...
- validators配置要点及No result defined for action报错解决方案
在做JavaEE SSH项目时,接触到validators验证. 需要了解validators配置,或者遇到No result defined for action 这个错误时,可查阅本文得到有效解决 ...
- mybatisplus ssm配置要点
本以为不难,但也捣鼓了大半天,记录要点如下: 在pom中引入mybatis plus相关包 <!-- mybatis-plus框架包 start --> <dependency> ...
- SQL 2016 AlwaysOn 无域AlwaysOn配置要点
环境准备: (1)操作系统:Windows Server 2016 Datacenter (2)SQL Server 2016 配置SQL 2016 AlwaysOn 要点 1,因为没有域所以需要在 ...
- Linux下Nginx+Tomcat负载均衡和动静分离配置要点
本文使用的Linux发行版:CentOS6.7 下载地址:https://wiki.centos.org/Download 一.安装Nginx 下载源:wget http://nginx.org/pa ...
- spring---aop 配置
第一种:注解配置AOP 注解配置AOP(使用 AspectJ 类库实现的),大致分为三步: 1. 使用注解@Aspect来定义一个切面,在切面中定义切入点(@Pointcut),通知类型(@Befor ...
- springAOP配置原理
什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引入 ...
随机推荐
- UVA - 12096 :The SetStack Computer
题目大意 用集合模拟计算机操作.每执行完一个操作,输出栈顶的集合大小,操作如下: PUSH:空集合压栈 DUP:将栈顶元素再次压栈 UNION:依次弹栈得a,b,求并集后压栈 INTERSECT:依次 ...
- 30例 | 一文搞懂python日期时间处理
前言 datetime是python的内置模块,用来处理日期和时间. 该模块常用的类有: 类名 功能说明 date 日期对象 time 时间对象 datetime 日期时间对象 timedelta 时 ...
- 活动回顾|阿里云云原生 Serverless 技术实践营 深圳站回放&PPT下载
11月24日"阿里云云原生 Serverless 技术实践营"深圳站圆满落幕.活动受众以关注 Serverless 技术的开发者.企业决策人.云原生领域创业者为主,活动形式为演讲 ...
- 基于 eBPF 的 Serverless 多语言应用监控能力建设
作者:竞霄 监控能力作为基础运维能力和核心稳定性措施,开发运维人员可以通过监控系统有效进行故障定位,预防潜在风险,分析长期趋势进行容量规划和性能调优,是软件开发生命周期中必不可少的一环.与此同时,Se ...
- 重磅发布丨从云原生到 Serverless,先行一步看见更大的技术想象力
(2022 云原生实战峰会) 2022年12月28日,以"原生万物 云上创新"为主题的第三届云原生实战峰会在线上举行. 会上,阿里云提出激活企业应用构建三大范式,并发布云原生可观测 ...
- poj 3246 简单线段树
线段树还真有点难写... #include <iostream> #include <cstdio> #include <cstring> #include < ...
- java基础-流程控制-day04
目录 1. if单分支 2. if else 多分支 3. if else双分支 4. 随机生成一定区间的整数 5 switch语句 6. while循环 7. for循环 8. break cont ...
- idea安装并使用maven依赖分析插件:Maven Helper
本文为博主原创,转载请注明出处: 在maven工程中,经常会查看maven的依赖树,在没使用该插件时,需要maven dependency:tree命令进行查看依赖树, 通过maven helper ...
- git仓库之gogs安装(docker版/二进制版)
二进制安装gogs tar zxf gogs_0.11.91_linux_amd64.tar.gz -C /data/gogs chown -R www.www /data/gogs su - www ...
- 05-逻辑仿真工具VCS-执行过程
Verilog Simulation Event Queue 主要了解VCS是如何处理交给它的代码的 Verilog的仿真事件队列,介绍VCS如何处理交给它的代码.VCS是Synopsys公司的,支持 ...