如何在 springbean 中注入完整的属性文件

发布于 2024-11-06 03:32:47 字数 354 浏览 5 评论 0原文

我有一个包含很多值的属性文件,我不想将它们单独列出在我的 bean 配置文件中。例如:

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

等等。

我想将所有内容完全注入 java.util.Properties 或更少地注入 java.util.Map。 有办法这样做吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

近箐 2024-11-13 03:32:47

对于 Java 配置,您可以使用如下内容:

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

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

如果为每个实例分配唯一的 bean 名称 (Qualifier),您还可以通过这种方式拥有多个属性。

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.

热风软妹 2024-11-13 03:32:47

是的,您可以使用 加载属性文件并将生成的 java.util.Properties 对象声明为 bean。然后您可以像注入任何其他 bean 属性一样注入它。

请参阅 Spring 手册的 C.2.2.3 节,及其示例:

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

请记住按照 这些说明

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.

毁梦 2024-11-13 03:32:47

对于 Java Config,使用 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;
}

然后,设置属性对象:

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

    myBean.setProperties(myProperties());

    ...
}

希望这对那些对 Java Config 方式感兴趣的人有所帮助。

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.

温柔戏命师 2024-11-13 03:32:47

使用 PropertyOverrideConfigurer 机制是可能的:

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

属性文件:

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

该机制在 3.8.2.2 示例:PropertyOverrideConfigurer

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

温暖的光 2024-11-13 03:32:47

这是 @skaffman 在这个问题中的回应的回声。当我将来尝试解决此问题时,我将添加更多详细信息以帮助他人和我自己。

可以通过三种方式注入属性文件

方法 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>

参考 ( 链接 )

方法 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"/>

参考 ( 链接 )

方法3

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

参考(链接

本质上,所有这些方法可以从属性文件中创建一个 Properties bean。您甚至可以使用 @Value 注入器直接从属性文件注入值

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

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; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文