gpt4 book ai didi

java - 有没有办法告诉 Lombok 不要复制字段注释?

转载 作者:行者123 更新时间:2023-12-05 02:34:26 24 4
gpt4 key购买 nike

我正在使用 Spring Boot 和 Lombock,我有一个用一些字符串属性初始化的 bean,并将它们提供给其他类:

@Getter
@Configuration
@PropertySource(value = "classpath:texts.properties")

public class TextProvider {

@Value("${some.text.value}")
private String text1;

@Value("${other.text.value}")
private String text2;
}

当 Lombok 为此类创建 getter 时,它会将 @Value 注释复制到 get 方法,导致 Spring 的 AutowiredAnnotationBeanPostProcessor 在系统启动时打印以下信息:“ Autowiring 注解应该只用在带有参数的方法上:” + 方法。

有没有办法不把这些注解复制到getter中?

最佳答案

我总是建议不要使用 @Value 注释从属性文件中检索值。为此使用属性类:

@ConfigurationProperties(...)
@Getter
public class TextProperties {
private String text1;
private String text2;
}

现在在你的配置类中阅读:

@Configuration
@PropertySource(value = "classpath:texts.properties")
@EnableConfigurationProperties(TextProperties.class)
public class TextProvider {
...
}

这样,您就可以在任何需要的地方 Autowiring TextProperties:

@Autowired
private TextProperties textProperties

阅读here获取更多信息,尤其是关于如何配置 @ConfigurationProperties 注释以及如何命名属性的信息。

关于java - 有没有办法告诉 Lombok 不要复制字段注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70766900/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com