【发布时间】:2020-10-26 23:33:28
【问题描述】:
希望你一切都好。 我直接开门见山:
我正在使用 Spring AOP 处理日志记录问题的 Spring Boot 应用程序。 它工作正常,但今天我发现我需要从 @ConfigurationProperties 注释的 bean 中记录一些属性(至少出于调试目的)。
我尝试使用切入点来捕获类构造函数及其设置器(Spring 使用反射填充字段)。它没有用,我相信这是由于反射的使用。
我使用以下代码解决了日志记录问题:
@Configuration
@Getter
@Setter
@ConfigurationProperties(prefix = "my.props")
@Slf4j
public class MyProps {
private String url;
private String tableName;
@PostConstruct
void logSomeFields() {
if (log.isInfoEnabled()) {
log.info("The URL is '{}' and the table name is '{}'",
url,
tableName
);
}
}
}
application.yml:
my:
props:
url: "http://localhost:8500"
tablename: "test_table"
我认为将日志记录代码放入属性 bean 是一种不好的做法, 你能告诉我是否有办法使用 Spring AOP,以便我可以将日志记录代码移出这个类吗?
提前谢谢你。
问候!
【问题讨论】:
标签: java spring-boot logging properties spring-aop