Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

spring-boot

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' ?

like image 992
Seffy Avatar asked Nov 02 '25 13:11

Seffy


1 Answers

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

@Value("${app.host}")
private String host;
like image 131
Hany Sakr Avatar answered Nov 04 '25 11:11

Hany Sakr