I want to access values provided in application.properties
, e.g.:
logging.level.org.springframework.web: DEBUG logging.level.org.hibernate: ERROR logging.file=${HOME}/application.log userBucket.path=${HOME}/bucket
I want to access userBucket.path
in my main program in a Spring Boot application.
Another very simple way to read application properties is to use @Value annotation. Simply annotation the class field with @Value annotation providing the name of the property you want to read from application. properties file and class field variable will be assigned that value.
Using the @Value Annotation The @Value annotation in spring boot reads the value from the application properties file and assigns it to a java variable. To read the property, the property key name must be provided in the @Value annotation.
You can use the @Value
annotation and access the property in whichever Spring bean you're using
@Value("${userBucket.path}") private String userBucketPath;
The Externalized Configuration section of the Spring Boot docs, explains all the details that you might need.
Another way is injecting org.springframework.core.env.Environment
to your bean.
@Autowired private Environment env; .... public void method() { ..... String path = env.getProperty("userBucket.path"); ..... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With