Spring: How to Use Values of Properties


I wrote the way to use values of properties, written in application.properties, in Spring.

In Spring, with annotation @Value, you can use the property values.

Sample Code

Make it available to use the AWS access key value written in properties file.

Of course you can write the code in the controller, but creating the class that has property value is smart. The following code is written in Kotlin.

Pass the @Value annotation, the key value in properties. When I create my own key and value in properties file, I start the key name with “my” to differentiate it from conflicting with one already used in libraries, in most case.

This scheme can be useful when you should use value according to the system environment, like production or development.

Above AwsConfig class has @Configuration annotation. @Component annotation works in the same way.

How to Use Config Class

Then, here’s a sample code using AwsConfig above.

Owing to @Autowired annotation, Spring assigns awsConfig Configration class instance automatically.