In this tutorial, I will show you how to read properties files in Spring using the util namespace.
First, I will create a Maven project as an example:
The contents of the Application class are as follows:
1 2 3 4 5 6 7 8 9 10 11 |
package com.huongdanjava.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); } } |
I will use Java 17 for this example:
1 2 3 4 |
<properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties> |
Spring dependency:
1 2 3 4 5 |
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>6.0.3</version> </dependency> |
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
1 2 3 4 5 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans> |
configuration.properties
1 |
app.author=Khanh Nguyen |
Now, I will configure the util namespace in Spring’s configuration file so that it can read the properties contained in the configuration.properties file for our application to use.
First, you need to declare using the util namespace as an attribute of the <beans> tag:
1 2 3 4 5 6 7 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> </beans> |
Next, you will use the util namespace to read the properties files as follows:
1 |
<util:properties id="properties" location="classpath:configuration.properties" /> |
In the above declaration, I used the <util:properties> tag to declare the path of the properties file.
The special with the util namespace is that with the above declaration, properties are read into an object of the java.util.Properties class with a variable named id, in my example it is the properties variable. When using properties with util namespace, the usage is also easier.
You can check this by adding some lines of code to the Application class as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.huongdanjava.spring; import java.util.Properties; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); Properties p = (Properties) context.getBean("properties"); System.out.println(p.get("app.author")); } } |
then the results will be:
The util namespace also supports our ignore-resource-not-found attribute so that our application does not have an exception when the path of the properties file does not exist.
Declare as follows:
1 |
<util:properties id="properties" location="classpath:configuration.properties" ignore-resource-not-found="true" /> |