Spring JavaConfig for java.util.Properties field

这一生的挚爱 提交于 2020-04-08 08:57:49

问题



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?


回答1:


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.




回答2:


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.




回答3:


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;
    }
}



回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/15387282/spring-javaconfig-for-java-util-properties-field

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!