Spring-AOP教程

1.建立工程

2.添加依赖

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

3.建立一个普通的Spring 增删改查功能

4.在驱动类里面加一个注解,启用AutoProxy自动代理

@EnableAspectJAutoProxy(proxyTargetClass=true)

5.创建一个面向切面类 ,添加一个注解

@Aspect   //表示是一个面向切面类
@Component/
	@Before(value = "execution(* com.example.demo.GoodsService.*(..))")//在所有方法执行之前,execution表示该包里的所有类都插入该方法
     public void beforeAdvice(JoinPoint joinPoint) { System.out.println("方法执行之前:" + joinPoint.getSignature());//获取签名 } @After(value = "execution(* com.example.demo.GoodsService.*(..))")//在所有方法执行之后调用 public void afterAdvice(JoinPoint joinPoint) { System.out.println("方法执行之前:" + joinPoint.getSignature()); } }

  

//在方法执行之前

猜你喜欢

转载自www.cnblogs.com/max-hou/p/12120514.html