In this tutorial, I will show you how to read file properties in Spring using the context namespace.
But first, let’s create an example project.
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> |
Class Application:
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"); } } |
In our 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 be configuring the context namespace in Spring’s configuration file so that it can read the properties in the configuration.properties file, and then our application can use it!
First, you need to declare using the context 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: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"> </beans> |
Now, we use the context namespace to read the properties file:
1 |
<context:property-placeholder location="classpath:configuration.properties" /> |
In this declaration, I used a <context:property-placeholder> tag of the context namespace to declare the path of the properties file.
If the path of the properties file does not exist, when running the application, we will encounter an error immediately. For now, let’s edit the path of the properties file as follows:
1 |
<context:property-placeholder location="classpath:configuration1.properties" /> |
When running you will encounter the following error:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Feb 23, 2018 5:18:11 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@45283ce2: startup date [Fri Feb 23 05:18:11 ICT 2018]; root of context hierarchy Feb 23, 2018 5:18:11 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [spring.xml] Feb 23, 2018 5:18:11 AM org.springframework.context.support.AbstractApplicationContext refresh WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [configuration1.properties] cannot be opened because it does not exist Exception in thread "main" org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [configuration1.properties] cannot be opened because it does not exist at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:153) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:164) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:693) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85) at com.huongdanjava.spring.Application.main(Application.java:9) Caused by: java.io.FileNotFoundException: class path resource [configuration1.properties] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180) at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:159) at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:99) at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:181) at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:162) at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:144) ... 7 more |
To ignore this error, you can declare another attribute for the <context:propery-holder> tag as follows:
1 |
<context:property-placeholder location="classpath:configuration1.properties" ignore-resource-not-found="true" /> |
Then, Spring only displays a message telling us there is a non-existent property file as follows:
1 2 3 4 5 6 |
Feb 23, 2018 5:18:41 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@45283ce2: startup date [Fri Feb 23 05:18:41 ICT 2018]; root of context hierarchy Feb 23, 2018 5:18:41 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [spring.xml] Feb 23, 2018 5:18:41 AM org.springframework.core.io.support.PropertiesLoaderSupport loadProperties INFO: Properties resource not found: class path resource [configuration1.properties] cannot be opened because it does not exist |
To read multiple properties files at the same time, you also use the location attribute of the <context: property-holder> tag and the path of the properties files will be separated by a comma.
For example, if I add a database.properties file in my example, I will declare the context namespace as follows:
1 |
<context:property-placeholder location="classpath:configuration.properties,classpath:database.properties" ignore-resource-not-found="true" /> |