博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring原理解析 之AOP
阅读量:2454 次
发布时间:2019-05-10

本文共 3057 字,大约阅读时间需要 10 分钟。

* AOP:【动态代理】 * 		指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式; *  * 1、导入aop模块;Spring AOP:(spring-aspects) * 2、定义一个业务逻辑类(MathCalculator);在业务逻辑运行的时候将日志进行打印(方法之前、方法运行结束、方法出现异常,xxx) * 3、定义一个日志切面类(LogAspects):切面类里面的方法需要动态感知MathCalculator.div运行到哪里然后执行; * 		通知方法: * 			前置通知(@Before):logStart:在目标方法(div)运行之前运行 * 			后置通知(@After):logEnd:在目标方法(div)运行结束之后运行(无论方法正常结束还是异常结束) * 			返回通知(@AfterReturning):logReturn:在目标方法(div)正常返回之后运行 * 			异常通知(@AfterThrowing):logException:在目标方法(div)出现异常以后运行 * 			环绕通知(@Around):动态代理,手动推进目标方法运行(joinPoint.procced()) * 4、给切面类的目标方法标注何时何地运行(通知注解); * 5、将切面类和业务逻辑类(目标方法所在类)都加入到容器中; * 6、必须告诉Spring哪个类是切面类(给切面类上加一个注解:@Aspect) * [7]、给配置类中加 @EnableAspectJAutoProxy 【开启基于注解的aop模式】 * 		在Spring中很多的 @EnableXXX; *  * 三步: * 	1)、将业务逻辑组件和切面类都加入到容器中;告诉Spring哪个是切面类(@Aspect) * 	2)、在切面类上的每一个通知方法上标注通知注解,告诉Spring何时何地运行(切入点表达式) *  3)、开启基于注解的aop模式;@EnableAspectJAutoProxy

搭建AOP案例

编写业务类

public class Business {
public int div(int i, int j){
System.out.println("业务逻辑执行"); return i/j; }}

编写切面类

import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.*;import java.util.Arrays;// @Aspect: 告诉Spring当前类是一个切面类@Aspectpublic class LogAspect {
//抽取公共的切入点表达式 //1、本类引用 //2、其他的切面引用用全路径名 @Pointcut("execution(public int com.example.demo.aop.Business.*(..))") public void pointCut(){
} @Before("pointCut()") public void logStart(JoinPoint joinPoint){
System.out.println("@Before running method:" + joinPoint.getSignature().getName() + " args:" + Arrays.asList(joinPoint.getArgs())); } @After("pointCut()") public void logEnd(){
System.out.println("@After running"); } @AfterReturning(value = "pointCut()", returning = "result") public void logReturn(JoinPoint joinPoint, Object result){
System.out.println("@AfterReturning running result:" + result); } @AfterThrowing(value = "pointCut()", throwing = "exception") public void logException(JoinPoint joinPoint, Exception exception){
System.out.println("@AfterThrowing running exception:" + exception); }}

编写配置类

import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.EnableAspectJAutoProxy;@Configuration@EnableAspectJAutoProxypublic class AOPConfig {
@Bean public Business business(){
return new Business(); } @Bean public LogAspect logAspect(){
return new LogAspect(); }}

编写测试类

@Test  public void test01() {
// 创建ioc容器 AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AOPConfig.class); Business bean = applicationContext.getBean(Business.class); bean.div(100, 5); // 关闭容器 applicationContext.close(); }

输出结果

@Before running method:div args:[100, 5]业务逻辑执行@After running@AfterReturning running result:20

异常输出结果

@Before running method:div args:[100, 0]业务逻辑执行@After running@AfterThrowing running exception:java.lang.ArithmeticException: / by zero

转载地址:http://lgdhb.baihongyu.com/

你可能感兴趣的文章
Raspberry Pi Zero W修复了网络遗漏
查看>>
公众号精选评论点赞_十大和编辑精选:三月评论
查看>>
python中flask_为什么以及如何在Python Flask中处理异常
查看>>
适合初学者的开源c需要项目_您的开源项目需要总裁吗?
查看>>
Python脚本可自动替换Scribus中的文本
查看>>
代码交互式图文_围绕交互式代码构建教室
查看>>
公众号精选评论点赞_十大和编辑精选:七月评论
查看>>
软齿面减速机抛开图_为了共同的目标而抛开自我
查看>>
维度诅咒_CEO自我的礼物和诅咒
查看>>
如何使用Jenkins运行JMeter
查看>>
angular 初探_初探Google的Science Journal应用
查看>>
关于开源软件研究的英文论文_关于开源公司软件的7个神话
查看>>
非传统营销 text_在传统营销失败的地方,社区推动的营销成功
查看>>
irc ubuntu_IRC入门
查看>>
Phire CMS:功能丰富的轻量级内容管理系统
查看>>
庆祝一下_加入我们,庆祝开放组织的一年
查看>>
dropbox为什么被屏蔽_Python社区和Dropbox为增加多样性而采取的步骤
查看>>
路由器搭建个人网站_PittMesh路由器归个人所有
查看>>
raspberry pi_Picademy:Raspberry Pi基金会的老师的免费专业发展
查看>>
retropie游戏版权_RetroPie的新网站和发行版,针对Linux的三款新游戏以及更多游戏新闻...
查看>>