Jan
Jan

Reputation: 101

How to inject complete propertiesfile in a springbean

I have a properties-file with a lot of values and I do not want to list them in my bean-configuration-file separately. E.g.:

<property name="foo">
    <value>${foo}</value>
</property>
<property name="bar">
    <value>${bar}</value>
</property>

and so on.

I imagine to inject all completely as java.util.Properties or less as a java.util.Map. Is there a way to do so?

Upvotes: 10

Views: 25684

Answers (5)

Lordbalmon
Lordbalmon

Reputation: 1714

This is an echo of @skaffman's response in this SO question. I am adding more details to help others and myself when I try to solve this in the future.

There are three ways to inject the property file

Method 1

<bean id="myProps" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <list>
            <value>classpath:com/foo/jdbc-production.properties</value>
        </list>
    </property>
</bean>

Reference ( link )

Method 2

<?xml version="1.0" encoding="UTF-8"?>
<beans
    ...
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="...
    ...
    http://www.springframework.org/schema/util/spring-util.xsd"/>
    <util:properties id="myProps" location="classpath:com/foo/jdbc-production.properties"/>

Reference ( link )

Method 3

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:com/foo/jdbc-production.properties" />
</bean>

Reference ( link )

Essentially, all the methods can create a Properties bean out of the properties file. You may even directly inject a value from the property file by using @Value injector

@Value("#{myProps[myPropName]}")
private String myField; 

Upvotes: 0

rustyx
rustyx

Reputation: 85341

For Java config you can use something like this:

@Autowired @Qualifier("myProperties")
private Properties myProps;

@Bean(name="myProperties")
public Properties getMyProperties() throws IOException {
    return PropertiesLoaderUtils.loadProperties(
        new ClassPathResource("/myProperties.properties"));
}

You can also have multiple properties this way, if you assign a unique bean name (Qualifier) to each instance.

Upvotes: 19

jelies
jelies

Reputation: 9290

For Java Config, use PropertiesFactoryBean:

@Bean
public Properties myProperties() {
    PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
    propertiesFactoryBean.setLocation(new ClassPathResource("/myProperties.properties"));
    Properties properties = null;
    try {
        propertiesFactoryBean.afterPropertiesSet();
        properties = propertiesFactoryBean.getObject();

    } catch (IOException e) {
        log.warn("Cannot load properties file.");
    }
    return properties;
}

And then, set the properties object:

@Bean
public AnotherBean myBean() {
    AnotherBean myBean = new AnotherBean();
    ...

    myBean.setProperties(myProperties());

    ...
}

Hope this helps for those interested in Java Config way.

Upvotes: 11

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298898

It's possible with the PropertyOverrideConfigurer mechanism:

<context:property-override location="classpath:override.properties"/>

Properties file:

beanname1.foo=foovalue
beanname2.bar.baz=bazvalue

The mechanism is explained in the section 3.8.2.2 Example: the PropertyOverrideConfigurer

Upvotes: 2

skaffman
skaffman

Reputation: 403481

Yes, you can use <util:properties> to load a properties file and declare the resulting java.util.Properties object as a bean. You can then inject that as you would any other bean property.

See section C.2.2.3 of the Spring manual, and their example:

<util:properties id="myProps" location="classpath:com/foo/jdbc-production.properties"

Remember to declare the util: namespace as per these instructions.

Upvotes: 14

Related Questions