springboot @EnableConfigurationProperties不起作用

时间:2017-07-10 02:10:15

标签: java spring-boot aspectj

我想使用

阅读application.properties
@EnableConfigurationProperties and @ConfigurationProperties.

以下是代码:

TraceLogProperties.java:

@ConfigurationProperties("tracelog")
public @Data class TraceLogProperties {

    private Boolean enable;

    ...some other fields.

}

TraceLogAutoConfigure.java:

@Configuration @Slf4j
@EnableConfigurationProperties(TraceLogProperties.class)
@AutoConfigureAfter(WebConfig.class)
public class TraceLogAutoConfigure {

    private @Autowired TraceLogProperties traceLogProperties;

    @ConditionalOnProperty(name = "tracelog.repository.enable", havingValue = "true")
    @Bean("repositoryInterceptor")
    public Interceptor repositoryInterceptor() {
        log.info("init repositoryInterceptor...");
        return new TraceLogInterceptor(this.traceLogProperties.getRepository());
    }

    @ConditionalOnProperty(name = "tracelog.service.enable", havingValue = "true")
    @Bean("serviceInterceptor")
    public Interceptor serviceInterceptor() {
        log.info("init serviceInterceptor...");
        return new TraceLogInterceptor(this.traceLogProperties.getService());
    }

    @ConditionalOnProperty(name = "tracelog.controller.enable", havingValue = "true")
    @Bean("controllerInterceptor")
    public Interceptor controllerInterceptor() {
        log.info("init controllerInterceptor...");
        return new TraceLogInterceptor(this.traceLogProperties.getController());
    }

    @ConditionalOnProperty(name = "tracelog.repository.enable", havingValue = "true")
    public @Bean Advisor repositoryAdvisor() {
        log.info("init repositoryAdvisor...");
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(this.traceLogProperties.getRepository().getPointcut());
        return new DefaultPointcutAdvisor(pointcut, repositoryInterceptor());
    }

    @ConditionalOnProperty(name = "tracelog.service.enable", havingValue = "true")
    public @Bean Advisor serviceAdvisor() {
        log.info("init serviceAdvisor...");
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(this.traceLogProperties.getService().getPointcut());
        return new DefaultPointcutAdvisor(pointcut, serviceInterceptor());
    }

    @ConditionalOnProperty(name = "tracelog.controller.enable", havingValue = "true")
    public @Bean Advisor controllerAdvisor() {
        log.info("init controllerAdvisor...");
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(this.traceLogProperties.getController().getPointcut());
        return new DefaultPointcutAdvisor(pointcut, controllerInterceptor());
    }
}

然后,当我启动应用程序时,它总是在

上发生NullPointerException
pointcut.setExpression(this.traceLogProperties.getController().getPointcut());

通过调试,我发现this.traceLogProperties为空。但是,如果我删除任何两个顾问,它将变为正常,如下所示:

@Configuration @Slf4j
@EnableConfigurationProperties(TraceLogProperties.class)
@AutoConfigureAfter(WebConfig.class)
public class TraceLogAutoConfigure {

    private @Autowired TraceLogProperties traceLogProperties;

    @ConditionalOnProperty(name = "tracelog.repository.enable", havingValue = "true")
    @Bean("repositoryInterceptor")
    public Interceptor repositoryInterceptor() {
        log.info("init repositoryInterceptor...");
        return new TraceLogInterceptor(this.traceLogProperties.getRepository());
    }

    @ConditionalOnProperty(name = "tracelog.service.enable", havingValue = "true")
    @Bean("serviceInterceptor")
    public Interceptor serviceInterceptor() {
        log.info("init serviceInterceptor...");
        return new TraceLogInterceptor(this.traceLogProperties.getService());
    }

    @ConditionalOnProperty(name = "tracelog.controller.enable", havingValue = "true")
    @Bean("controllerInterceptor")
    public Interceptor controllerInterceptor() {
        log.info("init controllerInterceptor...");
        return new TraceLogInterceptor(this.traceLogProperties.getController());
    }

    @ConditionalOnProperty(name = "tracelog.controller.enable", havingValue = "true")
    public @Bean Advisor controllerAdvisor() {
        log.info("init controllerAdvisor...");
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(this.traceLogProperties.getController().getPointcut());
        return new DefaultPointcutAdvisor(pointcut, controllerInterceptor());
    }
}

有什么想法吗?我错过了什么?

1 个答案:

答案 0 :(得分:1)

尝试将TraceLogProperties标记为组件:

@Component
@ConfigurationProperties("tracelog")
public @Data class TraceLogProperties {