Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to tell Lombok to NOT copy field annotations?

I am using Spring Boot and Lombock, and I have a bean that is initialized with some String properties, and provides them to other classes:

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

public class TextProvider {

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

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

When Lombok creates the getters for this class, it copies the @Value annotation to the get methods, causing Spring's AutowiredAnnotationBeanPostProcessor to print the following info on system startup: "Autowired annotation should only be used on methods with parameters: " + method.

Is there a way to not copy these annotations to the getter?

like image 396
Ido Raz Avatar asked Oct 19 '25 03:10

Ido Raz


1 Answers

I would always suggest to not use the @Value annotation for retrieving the values from property files. Use a property class for that:

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

Now read that in your configuration class:

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

That way, you can autowire the TextProperties everywhere you need it:

@Autowired
private TextProperties textProperties

Read here for some more information, especially on how to configure the @ConfigurationProperties annotation and how to name your properties.

like image 73
Seelenvirtuose Avatar answered Oct 20 '25 16:10

Seelenvirtuose



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!