效果:
@LoggerAnnotation("@beanName.beanMethod(#controllerParameter.getField(),'新增操作日志')")
1、注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LoggerAnnotation {
/**
* 操作
*
* @return 操作
*/
String value() default "";
}
2、切面
@Aspect
@Component
public class LoggerExecutor {
private final BasicsApplicationContextAware basicsApplicationContextAware;
public LoggerExecutor(BasicsApplicationContextAware basicsApplicationContextAware) {
this.basicsApplicationContextAware = basicsApplicationContextAware;
}
@Around("@annotation(LoggerAnnotation)")
public Object around(ProceedingJoinPoint point) throws Throwable {
final Object[] args = point.getArgs();
final Object proceed = point.proceed(args);
final MethodSignature signature = (MethodSignature) point.getSignature();
final LoggerAnnotation annotation = signature.getMethod().getAnnotation(LoggerAnnotation.class);
EvaluationContext context = basicsApplicationContextAware.createEvaluationContext(signature.getMethod(), args);
ExpressionParser parser = new SpelExpressionParser();
parser.parseExpression(annotation.value()).getValue(context);
return proceed;
}
}
3、ApplicationContextAware
public class BasicsApplicationContextAware implements ApplicationContextAware {
private BeanResolver resolver;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
resolver = new BeanFactoryResolver(applicationContext);
}
public final EvaluationContext createEvaluationContext(Method method, Object[] args) {
final StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(resolver);
LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer();
final String[] parameterNames = discoverer.getParameterNames(method);
for (int i = 0; i < args.length; i++) {
assert parameterNames != null;
context.setVariable(parameterNames[i], args[i]);
}
return context;
}
}
一个简单的使用 spring 容器内部 bean 和 请求传递参数的切面完成。
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>