10

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?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Jan
  • 101
  • 1
  • 1
  • 3

5 Answers5

19

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.

rustyx
  • 80,671
  • 25
  • 200
  • 267
14

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.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • Treat is like anyother bean. Use it within xml config using `ref=myProps` or Autowire a Properties to let Spring take care of it for you. – Lordbalmon Feb 15 '19 at 16:41
11

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.

jelies
  • 9,110
  • 5
  • 50
  • 65
  • Wow, that's a lot of code. Isn't there an easier way like the 1-liner XML config ``? – rustyx Jul 02 '15 at 19:30
  • @rustyx oh! I didn't find at that time any other way to do it, but [yours](http://stackoverflow.com/a/31193653/787375) is very nice! :) – jelies Jan 21 '16 at 13:21
2

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

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • thanks for your answer - my fault, the properties-file will be edited by a "not-developer" and shall be "easy to read" for the editor with no special background. Thus, names of beans wouldn't be the right way to use as key. – Jan May 09 '11 at 15:41
0

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; 
Community
  • 1
  • 1
Lordbalmon
  • 1,634
  • 1
  • 14
  • 31