【发布时间】:2020-04-21 18:43:57
【问题描述】:
我对 Spring Boot 和它的 AOP 风格非常陌生,但对其他语言和 AOP 框架的编程并不陌生。我不知道如何解决这一挑战。
我有一个简单的元数据装饰器:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface GreetingsMeta {
public float version() default 0;
public String name() default "";
}
它与依赖注入配合得很好:
public GreetingController(List<IGreetingService> greetings) throws Exception {
this.greetings = new HashMap<>();
greetings.forEach(m -> {
Class<?> clazz = m.getClass();
if (clazz.isAnnotationPresent(GreetingsMeta.class)) {
GreetingsMeta[] s = clazz.getAnnotationsByType(GreetingsMeta.class);
this.greetings.put(s[0].name(), m);
}
});
}
直到我应用了标准的日志记录方面:
@Aspect
@Component
public class LoggingAspect {
@Around("execution(* com.firm..*(..)))")
public Object profileAllMethods(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
String methodName = methodSignature.getName();
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
Object result = joinPoint.proceed();
stopWatch.stop();
LogManager.getLogger(methodSignature.getDeclaringType())
.info(methodName + " " + (stopWatch.getTotalTimeSeconds() * 1000) + " µs");
return result;
}
}
然后annotationsData的列表就变空了,连@Component注解都没有了。
示例元装饰类:
@Component
@GreetingsMeta(name = "Default", version = 1.0f)
public class DefaultGreetingsService implements IGreetingService {
@Override
public String message(String content) {
return "Hello, " + content;
}
}
我应该如何排除故障?
【问题讨论】:
标签: java spring-boot spring-aop