yN.
yN.

Reputation: 2257

Use @Value to inject value from properties file into @Configuration class

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.

Upvotes: 3

Views: 5431

Answers (1)

yN.
yN.

Reputation: 2257

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

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

Upvotes: 3

Related Questions