Spring的AOP注解开发

五种通知注解

  1. 前置通知: @Before
  2. 后置通知: @AfterReturing
  3. 环绕通知: @Around
  4. 最终通知: @After
  5. 异常通知: @AfterThrowing

注意1: 使用环绕通知时,默认是不执行切点方法的,需要手动引用,详情见例子中
注意2: spring4的执行顺序和spring5的执行顺序不同
spring4的执行顺序: @Around1,@Before,原方法,@Around2,@After,@AfterReturning
spring5.3.16的执行顺序: @Around前置,@Before,原方法,@AfterReturning,@After,@Around后置
注意: 出现异常时,后置通知,和后置环绕通知不输出,如果有异常通知,在此处输出异常通知

1. 导入jar包

* 先引入Spring框架开发的基本开发包
	* 再引入Spring框架的AOP的开发包
		* spring的传统AOP的开发的包
			* spring-aop-4.2.4.RELEASE.jar
			* com.springsource.org.aopalliance-1.0.0.jar
		
		* aspectJ的开发包
			* com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
			* spring-aspects-4.2.4.RELEASE.jar

2. 创建目标对象

package com.daben.service.impl;

import org.springframework.stereotype.Service;

import com.daben.service.IUserService;

@Service
public class UserServiceImpl implements IUserService {

	@Override
	public void save() {
		System.out.println("保存...");
		
	}

	@Override
	public void update() {
		System.out.println("更新...");
		
	}

}

3. 创建切面类

package com.daben.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyAspectAnnontation {
	// 自定应切入点(即取代通知时繁琐的切入点表达式,引用自定义的切入点时直接类名.切入点方法)
	@Pointcut(value="execution(public * com.daben.service.impl.UserServiceImpl.save())")
	public void fn(){}

	// 前置通知(切入点的表达式)
	@Before("MyAspectAnnontation.fn()")
	public void beforeNotice() {
		System.out.println("前置通知...");
	}
	// 后置通知
	@AfterReturning("MyAspectAnnontation.fn()")
	public void afterNotice() {
		System.out.println("后置通知");
	}
	// 环绕通知
	@Around("MyAspectAnnontation.fn()")
	public void aroundNotice(ProceedingJoinPoint joinPoint) {
		System.out.println("环绕通知111");
		try {
			joinPoint.proceed();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("环绕通知222");
	}
	// 最终通知
	@After("MyAspectAnnontation.fn()")
	public void lastNotice() {
		System.out.println("最终通知");
	}	
}

创建配置文件

在src下创建applicationContext.xml文件

<?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"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="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.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx.xsd">
	<!-- 开启组件扫描 -->
	<context:component-scan base-package="com.daben"/>
	<!-- 开启自动代理 -->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

SpringBoot中使用注解开发

  1. 导入AOP相关依赖:
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
  1. 遇上述的切面类一样,编写完切面类就可以使用了.

声明:
有一些博文是看的黑马程序员视频,然后跟着老师做的笔记
Spring是跟子路老师学的
特此感谢,写这些文章的目的是为了自己方便查阅

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值