【发布时间】:2018-01-04 08:16:39
【问题描述】:
我创建了一个自定义注释:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ValidateBeforeBuild {
}
还有一个方面为:
@Aspect
@Component
public class AspectForBuildInBuilders {
private static final Logger LOGGER = LoggerFactory.getLogger(AspectForBuildInBuilders.class);
@Before("@annotation(validateBeforeBuild )")
public void validateBusinessModelAdvice(JoinPoint jp, ValidateBeforeBuild validateBeforeBuild ) throws Throwable {
LOGGER.info("Executing class: {}", jp);
}
}
我有一个标有上述注释的build()。当我尝试调用build() 时,我没有收到来自validateBusinessModelAdvice() 的日志消息。我在其中一个配置类中也有@EnableAspectJAutoProxy。我错过了什么吗?是否需要更多信息?
【问题讨论】:
-
是在实现接口的类中构建,它是继承的吗?我的项目中有这个配置,它有效:@Before(value = "@annotation("annotations.methodBased.BeforeMethod) && atExecution()") public void beforeMethod(final JoinPoint jPoint) throws Throwable { 尝试添加 && atExecution ()
-
我询问了父类的接口,因为我注意到如果是,并且您的注释需要参数,那么只有在原始(在接口或父类中)方法定义是标有注释
-
是的。它在一个实现
Functional Interface的类中,称为IBuilder<T> -
好吧......我设法通过创建(为每个实现)一个单独的类来解决这个问题,确保我的原始类只将方法调用传递给该类,将注释添加到那里的方法定义。也许这会有所帮助,虽然我不确定我是否解释得足够清楚
-
您是否也启用了注释配置?我会在我的代码中尝试你的注释。看看我这边有没有问题
标签: java spring spring-boot spring-aop