This article explains how to get a value defined in properties file in Spring Boot Application.
How to read or access a value defined in the application.properties file in Spring Boot project.
application.properties
1 2 |
server.port=8989 spring.application.name=demoapp |
The @Value annotation is used to read the environment or application property value in Java code.
1 2 3 4 5 6 7 8 9 |
@Component public class MyBean { @Value("${spring.application.name}") private String applicationName; // ... } |
Another way is injecting org.springframework.core.env.Environment to your bean.
1 2 3 4 5 6 7 8 9 |
@Autowired private Environment env; .... public void method() { ..... String applicationName = env.getProperty("spring.application.name"); ..... } |