1.依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2.配置文件
test.key=testValue
3.注解
package com.qin.annotation;
import java.lang.annotation.*;
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AuthDemo {
String value() default "";
}
4.切面AOP
package com.qin.aspectj;
import com.qin.annotation.AuthDemo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class AuthDemoAspect implements EnvironmentAware {
Environment environment;
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Pointcut("@annotation(com.qin.annotation.AuthDemo)")
public void myPointCut() {
}
@Before(value = "myPointCut()")
public void check(){
System.out.println("check");
}
@After(value = "myPointCut()")
public void bye(){
System.out.println("bye");
}
@Around("myPointCut() && @annotation(authDemo)")
public void around(ProceedingJoinPoint joinPoint, AuthDemo authDemo){
String s = environment.resolvePlaceholders(authDemo.value());
System.out.println("未解析的值:"+authDemo.value());
System.out.println("解析后的值:"+ s);
Object proceed = null;
try {
proceed = joinPoint.proceed(new Object[]{s});
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return ;
}
}
5.测试
package com.qin.controller.MyResolveController;
import com.qin.annotation.AuthDemo;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping(value = "/api/")
public class MyResolveController{
@PostMapping(value = "/test")
@AuthDemo(value = "${test.key}")
public CommonResponse test(String param) {
CommonResponse response=new CommonResponse();
System.out.println(param);
return response;
}
}