Read properties files in Spring using @PropertySource annotation

I’ve introduced you to reading the properties of files in Spring using the PropertyPlaceholderConfigurer object, the util namespace, the context namespace. All of the above, we need to declare in Spring’s configuration file. In this tutorial, I introduce you all to another way to read the properties file in Spring: that is, using the @PropertySource annotation. How is it in details? Please read on.

First, I will create a Maven project as an example:

Read properties files in Spring using @PropertySource annotation

Spring framework dependency:

Class Application has the following content:

The HelloWorld class is the class that will use the properties declared in the configuration.properties file, which has the following content:

In my example, the spring.xml file is the Spring configuration file and the configuration.properties file is the application configuration file. The contents of these files are as follows:

spring.xml

configuration.properties

Now, I will use the @PropertySource annotation to read the properties in the configuration.properties file, then the HelloWorld class will use.


First, we need to use Spring’s auto component scan function to declare the HelloWorld object in the Spring container.

To do this, we need to declare the context:component-scan tag in the Spring configuration file with the com.huongdanjava.spring base package as follows:

and declare the @Component annotation in the HelloWorld class:

Now I will use the @PropertySource annotation to read the configuration.properties file, by declaring it in the HelloWorld class as follows:

With this declaration, Spring will use the path as the value of the @PropertySource annotation to find the properties file and read all the properties contained in this file. In my above declaration, I used the classpath variable, which corresponds to the /src/ main/resources path, and then the file name.

Now we can use the @Value annotation to inject the value of the “app.author” property into the value of the name attribute in the HelloWorld object.

Use the HelloWorld object in the application:

you will see the following results:

Read properties files in Spring using @PropertySource annotation

Annotation @PropertySource also supports us ignoreResourceNotFound, so that in case the properties file does not exist, no error occurs, declare the following:

Result:

Read properties files in Spring using @PropertySource annotation

In case you have multiple properties files to use in your application, you can use the @PropertySources annotation with multiple annotation declarations @PropertySource.

For example, you have an app.properties file as below:

Read properties files in Spring using @PropertySource annotation

with the following content:

Then, I can declare the @PropertySources annotation in the HelloWorld class as follows:

Of course, any property declared later will be used.

Result:

Read properties files in Spring using @PropertySource annotation

Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment