【问题标题】:Is there a way to tell Lombok to NOT copy field annotations?有没有办法告诉 Lombok 不要复制字段注释?
【发布时间】:2022-01-19 07:43:42
【问题描述】:

我正在使用 Spring Boot 和 Lombock,并且我有一个使用一些 String 属性初始化的 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 在系统启动时打印以下信息: "自动装配注解只能用于带参数的方法:" + 方法。

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

【问题讨论】:

    标签: java spring-boot lombok


    【解决方案1】:

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

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

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

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

    这样,您可以在任何需要的地方自动连接TextProperties

    @Autowired
    private TextProperties textProperties
    

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

    【讨论】:

    • 感谢@Seelenvirtuose,这很有帮助。如果我有多个属性文件,是否需要为每个属性创建一个属性类和一个提供者类? EnableConfigurationProperties 是一个类注释,所以似乎我不能有一个包含所有属性 bean 的配置文件。这是正确的吗?
    • @IdoRaz 您可以拥有任意数量的属性文件。您还可以拥有任意数量的此类属性类。每个人的数字甚至不必匹配。随心所欲地分割一切。注释 @EnableConfigurationProperties 也可以拆分。最合适的可能是有几个配置类,每个配置类都有一个特定属性集的启用器。
    【解决方案2】:

    为什么不在 All Args 构造函数中使用 @Value 注释?

    喜欢

    @Getter
    @Configuration
    @PropertySource(value = "classpath:texts.properties")
    public class TextProvider {
    
        private String text1;
        private String text2;
        
        public TextProvider(@Value("${some.text.value}") String text1,@Value("${other.text.value}") String text2){
            this.text1 = text1;
            this.text2 = text2;
        }
    
    }
    

    这样春天很开心,你仍然可以使用龙目岛的@Getter注解。

    【讨论】:

    • 这在有 2 个字段的示例中很好,但实际上我有很多字段,这会使构造函数变得讨厌,并使我定义每个字段两次 - 字段本身和构造函数,这就是龙目岛帮助避免的原因
    【解决方案3】:

    默认情况下,Lombok 不会创建已经编码过的方法,因此您可以简单地编写自己的 getter。

    缺点是 Lombok 在这种情况下会生成警告,所以最好通过指定 AccessLevel.NONE 来真正告诉 Lombok 不要考虑它,所以:

      @Getter
      @Configuration
      @PropertySource(value = "classpath:texts.properties")
    
      public class TextProvider {
    
            @Value("${some.text.value}")
            @Getter(AccessLevel.NONE)
            private String text1;
    
            @Value("${other.text.value}")
            @Getter(AccessLevel.NONE)
            private String text2;
    
            public String getText1()
            {
                return text1;
            }
    
            public String getText2()
            {
                return text2;
            }
    
    }
    

    当然,严格来说,对于这个例子,这使得在类级别使用 @Getter 完全没用 - 但这将是具有许多属性的类的一般情况的方法。

    【讨论】:

    • 是的,我想通了,但希望有办法避免它
    猜你喜欢
    • 2011-11-18
    • 2020-07-23
    • 1970-01-01
    • 2011-07-31
    • 2021-05-25
    • 2011-06-25
    • 2021-08-26
    • 1970-01-01
    相关资源
    最近更新 更多