-1
を動作しない、私はここでspringbootの@EnableConfigurationPropertiesが
@EnableConfigurationProperties and @ConfigurationProperties.
を使用してapplication.propertiesを読みたいコードです:
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
がnullであることがわかりました。しかし、2人のアドバイザーを削除すると、次のように正常になります。
@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());
}
}
ご存知ですか?私は何が欠けていますか?