[Java Spring] @Value & override properties by environment variables

We can have some properties defined inside application.properties or application.yml file.

application.properties:

app.name=Frank
app.greeting=Hello

 

We can get those value by @Value:

@Configuration
@PropertySource("classpath:application.properties")
public class ApplicationConfig {

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

   ...

}

@PropertySource is used to defined where to looking for application.properties file. Normally it should be located in 'resources' folder.

'classpath': points to 'resources' folder.

 

Environment variable:

we can also override those properties by environment varialbe:

posted @ 2020-12-04 15:50  Zhentiw  阅读(99)  评论(0编辑  收藏  举报