springboot中面向切面编程

springboot中面向切面编程

AOP面向切面编程

AOP:spring中提出AOP概念,就是通过动态地为项目中某些类在运行过程中创建代理对象,在代理对象中完成附加功能 | 额外功能;保证在处理业务时能够更加专注于自己的业务逻辑开发。

概念

  • 1)通知(Advice):除了目标方法以外的操作称之为通知,包括日志通知,性能通知,事务通知等;
  • 2)切入点(pointcut):用来告诉项目中哪些类中哪些方法在执行过程中加入通知;
  • 3)切面(Aspect):通知 + 切入点

Spring中的AOP编程,步骤

  1. 开发通知,前置,后置,环绕,异常
  2. 配置通知 并 组装切面
    <bean id class="xxxAdvice">
    <aop:config>
    	<aop:pointcut expression="exection | within | annotation | args   ">
    	<aop:advisor advice-ref=" " pointcut-ref=" ">
    </aop:config>
    

SpringBoot中的切面编程

需要先引入spring-boot-starter-aop

相关注解:@Aspect :修饰范围作用在类上, 代表这个类是一个切面配置类。相当于<aop:config>

通知相关注解:

	@Before:前置通知,修饰范围:用在方法上,或者类上;Value属性:用在哪个方法上
	@After:后置通知,修饰范围:用在方法上,或者类上;Value属性:用在哪个方法上
	@Around:环绕通知,修饰范围:用在方法上,或者类上;Value属性:用在哪个方法上
	@Order:顺序控制,修饰范围:用在方法上,或者类上,在springboot中只能作用在类上; Value属性:int,int值越小,迁址通知的执行顺序越靠前,后置通知则相反,因为多个aop切面同样是一个“栈式”的结构,先进后出。
	修饰范围:用在方法上,或者类上;Value属性:用在哪个方法上
	
	@Before和@After实例:
		@Component
		@Aspect
		public class MyAspect {
		    @Before(value="within(com.test.service.*ServiceImpl)")
		    public void before(){
		        System.out.println("============前置通知=============");
		    }
		}
		
	如果在@Before和@After中想要获取运行时方法的相关信息,在自定义通知方法中加入JointPoint参数:
		//    @After(value = "within(com.test.service.*ServiceImpl)")
		//    public void after(JoinPoint joinPoint){
		//        System.out.println("============后置通知=============");
		//        System.out.println("当前执行方法名: "+joinPoint.getSignature().getName());
		//        System.out.println("当前执行方法参数: "+joinPoint.getArgs());
		//        System.out.println("当前目标对象: "+joinPoint.getTarget());
		//    }

	@Around:执行目标方法之前会进入环绕通知,环绕通知放行之后,才会执行目标方法,执行目标方法之后,再回到环绕通知。
   (注意这里要定义成Object类型的方法,因为如果执行方法有结果返回的话,需要通过环绕通知的Object返回结果。)
		@Around(value = "within(com.wuhaotian.service.*ServiceImpl)")
	    public Object around(ProceedingJoinPoint proceedingJoinPoint)  {
	        System.out.println("=============在方法执行前进入环绕通知=============");
	        System.out.println("当前执行方法名: "+proceedingJoinPoint.getSignature().getName());
	        System.out.println("当前执行方法参数: "+proceedingJoinPoint.getArgs());
	        System.out.println("当前目标对象: "+proceedingJoinPoint.getTarget());
	        //放行执行目标方法
	
	        try {
	            Object proceed = proceedingJoinPoint.proceed();
	            System.out.println("=============在方法执行后进入环绕通知=============");
	            return proceed;
	        } catch (Throwable throwable) {
	            System.out.println("异常处理");
	            return null;
	        }
	    }
	
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值