In some previous tutorials, I already introduced with you all about how to read properties files in Spring using PropertyPlaceholderConfigure object, namespace util, or namespace context. So, how we can use these properties in the Spring configuration file? In this tutorial, I will guide you all about that.
First of all, I will create a new Maven project for example:
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 framework dependency:
1 2 3 4 5 |
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>6.0.3</version> </dependency> |
HelloWorld class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.huongdanjava.springusingpropertiesinspringxml; public class HelloWorld { private String name; public void print() { System.out.print("Hello " + name); } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
- Application class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.huongdanjava.springusingpropertiesinspringxml; 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"); HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld"); helloWorld.print(); } } |
- configuration.properties
1 |
name=Khanh |
- spring.xml
1 2 3 4 5 6 7 8 9 10 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:configuration.properties" /> </beans> |
Here, I used namespace context to read the configuration.properties.
To use these properties in the Spring configuration file, we need to declare them with the following syntax:
1 |
${property_name} |
With this declaration, Spring will ingest the value of the property into beans in the Spring configuration file automatically.
For example, I will declare the HelloWorld object as a bean in the Spring configuration file with the value of the name attribute as the value of the name property in configuration.properties, as below:
1 2 3 |
<bean id="helloWorld" class="com.huongdanjava.springusingpropertiesinspringxml.HelloWorld"> <property name="name" value="${name}" /> </bean> |
Result:
Note, if a property was declared in multiple properties file then the value of this property will be in the last declaration properties file.
For example, I add more properties file, named configuration-override.properties
with the following content:
1 |
name=Huong Dan Java |
and declare using this property in namespace context:
1 |
<context:property-placeholder location="classpath:configuration.properties,classpath:configuration-override.properties" /> |
then when running, the result is as below: