Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you configure a Spring bean container to load a Java property file?

How do you configure a Spring bean container (or application context) to load a Java property file?

JavaWorld article Smartly Load Your Properties explains how to load property files from the classpath using one of the following resource processing methods in the standard Java library:

ClassLoader.getResourceAsStream ("some/pkg/resource.properties");
Class.getResourceAsStream ("/some/pkg/resource.properties");
ResourceBundle.getBundle ("some.pkg.resource");

How can you do the same using a Spring bean container?

like image 544
Derek Mahar Avatar asked Jun 04 '10 15:06

Derek Mahar


People also ask

How do you inject Java Util properties into Spring beans?

Create a configuration class and define the properties bean in it as shown below. Create a spring bean class and inject properties into it using @Autowired and @Qualifier annotations. Create main class and run application.

How are beans configured in Spring?

There are several ways to configure beans in a Spring container. Firstly, we can declare them using XML configuration. We can also declare beans using the @Bean annotation in a configuration class. Finally, we can mark the class with one of the annotations from the org.


4 Answers

The Spring Framework Reference Documentation (2.5.x) gives two examples of how to load a property file into a bean container, one before the release of version 2.5 and a more concise way using the <util:properties/> function that was introduced in version 2.5:

Before version 2.5:

<!-- creates a java.util.Properties instance with values loaded from the supplied location -->
<bean id="jdbcConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="location" value="classpath:com/foo/jdbc-production.properties"/>
</bean>

After version 2.5:

<!-- creates a java.util.Properties instance with values loaded from the supplied location -->
<util:properties id="jdbcConfiguration" location="classpath:com/foo/jdbc-production.properties"/>

Note that in order to use <util:properties/>, you must declare the util namespace and schema location in the preamble at the top of your Spring XML configuration file:

<?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-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">

<!-- <bean/> definitions here -->

</beans>
like image 102
Derek Mahar Avatar answered Sep 19 '22 15:09

Derek Mahar


Your beans.xml file should have a PropertyPlaceholderConfigurer:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:some/pkg/resource.properties</value>
        </list>
    </property>
    <!-- Default values for backwards compatibility -->
    <property name="properties">
        <props>
            <prop key="name">value</prop>
        </props>
    </property>
</bean>

And then you can reference the properties as such elsewhere in the beans.xml:

<bean class="${blah}">
    ....
<bean>

For an article about this, check out http://almaer.com/blog/spring-propertyplaceholderconfigurer-a-nice-clean-way-to-share

like image 29
Edward Dale Avatar answered Sep 21 '22 15:09

Edward Dale


For example via the PropertiesFactoryBean. Use the PropertyPlaceholderConfigurer to configure beans in the context via properties.

You will find PropertyPlaceholderConfigurer examples in the other answers. Here is a PropertiesFactoryBean example :

<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="location" value=classpath:config/applicationConfig.properties"/>
</bean>
like image 45
Timo Westkämper Avatar answered Sep 19 '22 15:09

Timo Westkämper


There's this thing called a PropertyPlaceholderConfigurer, you can use it to inject your beans with values from a properties file, like this:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>classpath:com/foo/jdbc.properties</value>
    </property>
</bean>

<bean id="dataSource" destroy-method="close"
      class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>
like image 22
Nathan Hughes Avatar answered Sep 21 '22 15:09

Nathan Hughes