2
votes

I have a value stored in an application.properties file in the resources directory. I want to inject this value in a class with the @Configuration annotation.

@Configuration
@RequiredArgsConstructor
public class Xconfig {

    @Value("${x}")
    private final String x;

}

application.properties:

x=hello

This works for @Component/@Service classes but not for the @Configuration. Error message:

Parameter 1 of constructor in xConfig required a bean of type 'java.lang.String' that could not be found.

1
either remove final from String x, or declare your constructor manually (and remove @RequiredArgsConstructor) and attach @Value to the parameter. - mrkernelpanic
How is this cdi related? - Kukeltje

1 Answers

1
votes

Replacing the @RequiredArgsConstructor annotation with an own constructor solves the issue.

public Xconfig(@Value("${x}") final String x) {
    this.x = x;
}