@SpringBootApplication @PropertySource("classpath:test.properties") public class PropertyTestApplication {
public static void main(String[] args) {
SpringApplication.run(PropertyTestApplication.class, args);
}
}
You can add it to Environment by specifying the path of the property file in the argument of @PropertySource ().
@PropertySource must be declared with @Configuration,
Since @Configuration is declared in @SpringBootApplication, there is no need to declare it specially.
[Click here for @PropertySource reference](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/PropertySource.html)
# Get it with env.getProperty ().
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class PropertyGetComponent {
@Autowired
Environment env;
public void printProperty() {
String value = env.getProperty("test.property.key");
System.out.println("The obtained value is[ " + value + " ]is.");
}
}
The execution result of printProperty () is ↓
The obtained value is[ test value ]is.
I was able to get the value from the property file added by env.getProperty (): blush:
Recommended Posts