Seffy
Seffy

Reputation: 1085

Spring Boot - can't read custom properties from application.yml

I added to my application.yml the following section:

app: 
  host: server.com

I injected the Environment to my class (RestController):

@Autowired
private Environment environment;

But reading the value returns null:

System.out.println( environment.getProperty( "app.host" ) );

What is the correct way to achieve that? Will it be the same for nested properties like 'app.config.serviceA.host' ?

Upvotes: 4

Views: 7695

Answers (1)

Hany Sakr
Hany Sakr

Reputation: 2929

Try using a class variable with the value annotation inside any annotated class @Component, @Repository, etc..

@Value("${app.host}")
private String host;

Upvotes: 4

Related Questions