In this short article, we will discuss different ways to read property files in Spring-based web applications.
Reading Files using Bean Namespace
Below example shows reading properties from a file. We use PropertyPlaceholderConfigurer bean and it's location property used to load property files. Here is how properties can be read from a file using the bean namespace:
<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">
<!-- reading the properties, Spring infrastructure bean is exposed -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:db/datasource.properties"/>
</bean>
<!-- here the values are injected into a datasource -->
<bean id="dataSource1"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
</beans>
Reading Files using Context Namespace
Here is how properties can be read from a file using the context namespace:
<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">
<!-- Spring infrastructure is not visible anymore -->
<context:property-placeholder location="classpath:db/datasource.properties" />
<!-- here the values are injected into a datasource -->
<bean id="dataSource2"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
</beans>
Reading Files using Util Namespace
Here is how properties can be read from a file using the util namespace:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- Spring infrastructure is not visible anymore -->
<util:properties id="dbProp" location="classpath:db/datasource.properties"/>
<bean id="dataSource3"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="#{dbProp.driverClassName}"/>
<property name="url" value="#{dbProp.url}"/>
<property name="username" value="#{dbProp.username}"/>
<property name="password" value="#{dbProp.password}"/>
</bean>
</beans>
Reading Files using Spring Java-based Configuration - @PropertySource annotation
The @PropertySource annotation to externalize your configuration to a properties file.
In this example, we are reading database configuration from config.properties file and set these property values to DataSourceConfig class using Environment.
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
@Configuration
@PropertySource("classpath:config.properties")
public class ProperySourceDemo implements InitializingBean {
@Autowired
Environment env;
@Override
public void afterPropertiesSet() throws Exception {
setDatabaseConfig();
}
private void setDatabaseConfig() {
DataSourceConfig config = new DataSourceConfig();
config.setDriver(env.getProperty("jdbc.driver"));
config.setUrl(env.getProperty("jdbc.url"));
config.setUsername(env.getProperty("jdbc.username"));
config.setPassword(env.getProperty("jdbc.password"));
System.out.println(config.toString());
}
}
Spring @PropertySource Annotation Placeholders Example
Any ${…} placeholders present in a @PropertySource resource location will be resolved against the set of property sources already registered against the environment.
For example:
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
@Configuration
@PropertySource("classpath:/com/${my.placeholder:default/path}/config.properties")
public class ProperySourceDemo implements InitializingBean {
@Autowired
Environment env;
@Override
public void afterPropertiesSet() throws Exception {
setDatabaseConfig();
}
private void setDatabaseConfig() {
DataSourceConfig config = new DataSourceConfig();
config.setDriver(env.getProperty("jdbc.driver"));
config.setUrl(env.getProperty("jdbc.url"));
config.setUsername(env.getProperty("jdbc.username"));
config.setPassword(env.getProperty("jdbc.password"));
System.out.println(config.toString());
}
}
Reading Multiple Property Files using @PropertySources Annotation
Introduces new @PropertySources to support Java 8 and a better way to include multiple properties files.
@Configuration
@PropertySources({
@PropertySource("classpath:config.properties"),
@PropertySource("classpath:db.properties")
})
public class AppConfig {
//...
}
Allow @PropertySource to ignore the not found properties file.
@Configuration
@PropertySource("classpath:missing.properties")
public class AppConfig {
//...
}
If missing.properties is not found, the system is unable to start and throws FileNotFoundException
Caused by: java.io.FileNotFoundException:
classpath resource [missiong.properties] cannot be opened because it does not exist
In Spring 4, you can use ignoreResourceNotFound to ignore the not found properties file
@Configuration
@PropertySource(value="classpath:missing.properties", ignoreResourceNotFound=true)
public class AppConfig {
//...
}
@PropertySources({
@PropertySource(value = "classpath:missing.properties", ignoreResourceNotFound=true),
@PropertySource("classpath:config.properties")
})
Comments
Post a Comment
Leave Comment