我想阅读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());
    }
}


任何想法?我想念什么?

最佳答案

尝试将TraceLogProperties标记为组件:

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

10-07 20:47