In some previous tutorial, I already introduce with you all about how to read properties file in Spring using PropertyPlaceholderConfigure object, namespace util or namespace context. So, how we can use these properties in Spring configuration file. In this tutorial, I will guide you all about that.
First of all, I will create new Maven project for example:
Spring framework dependency:
1 2 3 4 5 |
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.9.RELEASE</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 |
<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 Spring configuration file, we need declare them with following syntax:
1 |
${property_name} |
With this declaration, Spring will ingest value of the property into beans in Spring configuration file automatically.
Example, I will declare HelloWorld object as a bean in Spring configuration file with the value of name attribute is the value of property name 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.
Example, I add more properties file, named configuration-override.properties
with following content:
1 |
name=Huong Dan Java |
and declare using this properties in namespace context:
1 |
<context:property-placeholder location="classpath:configuration.properties,classpath:configuration-override.properties" /> |
then when running, result as below: