SpringBoot之AOP

时间:2023-03-08 22:17:45
SpringBoot之AOP

AOP:面向切面编程,相当于OOP面向对象编程。

Spring的AOP的存在目的是为了解耦,AOP可以让一组类共享相同的行为。

Spring支持AspectJ的注解切面编程:

(1)使用@Aspect声明是一个切面

(2)使用@Afte、@Before、@Around定义通知/建言,可以直接将拦截规则(切点)作为参数。

(3)为了使切点服用,可以使用@PointCut专门定义拦截规则,然后在@After、@Before、   @Advance的参数中调用。

(4)其中符合条件的每一个被拦截处为 连接点(JoinPoint)

eg:

 package com.wisely.heighlight_spring4.ch1.aop;

 import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; /**
*
* Makedate:2018年3月21日 下午1:01:48
* @author dong
* @version %I%, %G%
* @since 1.0
* 注解类(jdk1.5之后没设定特殊关键字而已)
* 功能:拦截规则的注解
* retention:保留 policy:政策 方针
*
*
**/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
String name();
}
 package com.wisely.heighlight_spring4.ch1.aop;

 import org.springframework.stereotype.Service;

 @Service
public class DemoAnnotationService {
@Action(name="注解式拦截的add操作")
public String add() {
return "haha1";
}
}
 package com.wisely.heighlight_spring4.ch1.aop;

 import org.springframework.stereotype.Service;

 /**
*
* 编写使用方法规则被拦截类
*
* Makedate:2018年3月21日 下午3:08:39
* @author dong
* @version %I%, %G%
* @since 1.0
*
*
*/
@Service
public class DemomethodService {
public String add() {
return "hehe2";
}
}
 package com.wisely.heighlight_spring4.ch1.aop;

 import java.lang.reflect.Method;

 import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; @Aspect //注解声明是一个切面 切面=通知+切点
@Component //让这个切面成为spring容器管理的Bean
public class LogAspect {
@Pointcut("@annotation(com.wisely.heighlight_spring4.ch1.aop.Action)") //注解声明切点
public void annotationPointCut() {}; @After("annotationPointCut()") //注解声明通知 并使用@PointCut定义的切点
public void after(JoinPoint joinpoint) {
MethodSignature signature = (MethodSignature)joinpoint.getSignature();
Method method = signature.getMethod();
Action action = method.getAnnotation(Action.class);
System.out.println("注解式拦截:"+action.name()); //通过反射可以获得注解上的属性,做日志记录相关操作
} @Before("execution(* com.wisely.heighlight_spring4.ch1.aop.DemomethodService.*(..))")
//@Before声明一个通知 这个通知直接使用拦截规则作为参数
public void before(JoinPoint joinpoint) {
MethodSignature signature = (MethodSignature)joinpoint.getSignature();
Method method = signature.getMethod();
System.out.println("方法规则式拦截,"+method.getName());
}
}
 package com.wisely.heighlight_spring4.ch1.aop;

 import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration
@ComponentScan("com.wisely.heighlight_spring4.ch1.aop")
@EnableAspectJAutoProxy //开启spring对aspecJ的支持
public class AopConfig { }
 package com.wisely.heighlight_spring4.ch1.aop;

 import org.springframework.context.annotation.AnnotationConfigApplicationContext;

 public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);
DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);
DemomethodService demomethodService = context.getBean(DemomethodService.class);
String add = demoAnnotationService.add();
System.out.println("add---"+add); String add2 = demomethodService.add();
System.out.println("add2---"+add2);
context.close();
}
}