Spring JavaConfig for java.util.Properties field
Asked Answered
P

6

16


Can you please tell me how to use Spring Javaconfig to directly load/autowire a properties file to a java.util.Properties field?

Thanks!

Later edit - still searching for the answer: Is it possible to load with Spring JavaConfig a properties file directly into a java.util.Properties field?

Parthinia answered 13/3, 2013 at 13:52 Comment(0)
S
13

The XML base Way:

in spring config:

<util:properties id="myProperties" location="classpath:com/foo/my-production.properties"/>

in your class:

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

JavaConfig Only

It looks like there is an annotation:

@PropertySource("classpath:com/foo/my-production.properties")

Annotating a class with this will load the properties from the file in to the Environment. You then have to autowire the Environment into the class to get the properties.

@Configuration
@PropertySource("classpath:com/foo/my-production.properties")
public class AppConfig {

@Autowired
private Environment env;

public void someMethod() {
    String prop = env.getProperty("my.prop.name");
    ...
}

I do not see a way to directly inject them into the Java.util.properties. But you could create a class that uses this annotation that acts as a wrapper, and builds the properties that way.

Scaleboard answered 13/3, 2013 at 15:3 Comment(4)
Thank you, but I am interested in how to get this result by using only Spring JavaConfig, without the xml file...Parthinia
@Parthinia oops. Edited my post.Scaleboard
Thank you for the answer. I have a special situation where I definetely need a Properties field. I will keep in mind your idea with the wrapper, but I wonder if there isn't a simpler solution...:)Parthinia
@Parthinia check my answer belowCompensation
C
6

declare a PropertiesFactoryBean.

@Bean
public PropertiesFactoryBean mailProperties() {
    PropertiesFactoryBean bean = new PropertiesFactoryBean();
    bean.setLocation(new ClassPathResource("mail.properties"));
    return bean;
}

Legacy code had following config

<bean id="mailConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="location" value="classpath:mail.properties"/>
</bean>

Converting that to Java config is super easy as shown above.

Compensation answered 30/9, 2015 at 10:7 Comment(0)
F
2

It is an old subject but there are also a more basic solution.

@Configuration
public class MyConfig {
    @Bean
    public Properties myPropertyBean() {
        Properties properties = new Properties();
        properties.load(...);
        return properties;
    }
}
Floreneflorentia answered 29/12, 2014 at 13:8 Comment(0)
L
1

There is also this approach for injecting properties directly using xml configurations. The context xml has this

<util:properties id="myProps" location="classpath:META-INF/spring/conf/myProps.properties"/>

and the java class just uses

@javax.annotation.Resource
private Properties myProps;

Voila!! it loads. Spring uses the 'id' attribute in xml to bind to the name of variable in your code.

Lapp answered 1/10, 2013 at 12:23 Comment(0)
L
1

You can try this

@Configuration  
public class PropertyConfig { 

 @Bean("mailProperties")  
 @ConfigurationProperties(prefix = "mail")   
  public Properties getProperties() {
     return new Properties();  
  }

}

Make sure to define properties in application.properties

Locally answered 1/2, 2021 at 12:22 Comment(0)
S
0

application.yml:

root-something:
    my-properties:
        key1: val1
        key2: val2

Your type-safe pojo:

import java.util.Properties;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "root-something")
public class RootSomethingPojo {

    private Properties myProperties;

Your container configuration:

@Configuration
@EnableConfigurationProperties({ RootSomethingPojo .class })
public class MySpringConfiguration {

This will inject the key-value pairs directly into the myProperties field.

Sphygmograph answered 30/3, 2017 at 9:53 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.