简体   繁体   English

在 spring 中加载属性文件

[英]loading properties file in spring

One of our team has implemented loading properties this way (see pseudo code below) and advises this approach is right as the client application using this is free to keep the properties in any file.我们的一个团队已经以这种方式实现了加载属性(参见下面的伪代码),并建议这种方法是正确的,因为使用这种方法的客户端应用程序可以自由地将属性保存在任何文件中。 Contrary to the widely used propertyplaceholderconfigurer.与广泛使用的 propertyplaceholderconfigurer 相反。

application-context.xml应用程序上下文.xml

<bean class="com.mypackage.Myclass">
<property name="xml" value="classpath:"{com.myapp.myproperty1}"> </property> 
</bean>

config.properties config.properties

com.myapp.myproperty1=data.xml

edit: I should have added it is data.properties and not data.xml.编辑:我应该添加它是 data.properties 而不是 data.xml。 We want to load a property file (this property file is given in the config.properties as a "property". com.myapp.myproperty1=data.properties我们要加载一个属性文件(这个属性文件在 config.properties 中作为“属性”给出。com.myapp.myproperty1=data.properties

java class java类

import org.springframework.core.io.Resource;
public class Myclass {

private Resource xmlField;

// setter & getter methods..

}

Is it right to use spring core.io.Resource?使用 spring core.io.Resource 对吗?

Another reason is the client application wants to load a environment specific configuration.另一个原因是客户端应用程序想要加载特定于环境的配置。 I suggested use the propertyconfigurer and use maven profiles to generate the environment specific build我建议使用 propertyconfigurer 并使用 maven 配置文件来生成特定于环境的构建

Can you please advise which one suits which case?你能建议哪一种适合哪种情况吗? and if it differs in different scenarios, please help me point out them?如果它在不同的场景中有所不同,请帮我指出它们?

thanks谢谢

You can put the properties in any file and still use PropertyPlaceholderConfigurer .您可以将属性放在任何文件中,并且仍然使用PropertyPlaceholderConfigurer Here's an example that satisfies both your coworker's concerns and your desire for environment specific stuff:这是一个既满足您同事的担忧又满足您对环境特定内容的渴望的示例:

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <!-- default settings -->
            <value>classpath:MyCompany.properties</value>
            <!-- environment-specific settings -->
            <value>classpath:MyCompany.${mycompany.env:dev}.properties</value>
            <!-- keep your coworker happy -->
            <value>classpath:${mycoworker}</value>
            <!-- allows emergency reconfiguration via the local file system -->
            <value>file:///${user.home}/MyCompany.properties</value>
        </list>
    </property>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="ignoreResourceNotFound" value="true" />
    <!-- should be validated separately, in case users of the library load additional properties -->
    <property name="ignoreUnresolvablePlaceholders" value="false"/> 
</bean>

If you pass in no -D arguments, then you'll pick up the following properties files, where properties in the later files overwrite previously determined values.如果不传入-D参数,则将选取以下属性文件,其中后面文件中的属性会覆盖先前确定的值。

  1. MyCompany.properties off the classpath MyCompany.properties 离开类路径
  2. MyCompany.dev.properties off the classpath MyCompany.dev.properties 离开类路径
  3. $HOME/MyCompany.properties if it exists $HOME/MyCompany.properties 如果存在

To swap in a production config for #2, just pass -Dmycompany.env=prod to java.要为 #2 交换生产配置,只需将-Dmycompany.env=prod传递给 java。 Similarly your coworker can pass -Dmycoworker=/some/path/config.properties if he/she wants.同样,您的同事可以通过-Dmycoworker=/some/path/config.properties如果他/她愿意。

I'm not sure why a PropertyPlaceholderConfigurator wouldn't have been the correct choice.我不确定为什么PropertyPlaceholderConfigurator不是正确的选择。

I've almost always handled environment-specific configs via a customized PPC that can either (a) get a -D parameter on startup, and/or (b) use the machine name, to decide which property file to load.我几乎总是通过定制的 PPC 处理特定于环境的配置,它可以 (a) 在启动时获取-D参数,和/或 (b) 使用机器名称来决定加载哪个属性文件。

For me, this is more convenient than bundling the information in via Maven, since I can more easily test arbitrary configurations from whatever machine I'm on (using a -D property).对我来说,这比通过 Maven 捆绑信息更方便,因为我可以更轻松地从我使用的任何机器上测试任意配置(使用-D属性)。

+1 for Dave's suggestion. +1 为戴夫的建议。 You should be using PropertyPlaceholderConfigurer for loading\\reading properties.您应该使用 PropertyPlaceholderConfigurer 来加载\\读取属性。 Here is the example i just pulled out from my previous project if you wonder how to use this.如果您想知道如何使用它,这是我刚刚从我之前的项目中提取的示例。 This example is for loading multiple properties files but the concept is same.此示例用于加载多个属性文件,但概念相同。 Good luck.祝你好运。

<bean id="projectProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <list>
            <value>classpath:config.properties</value>
        </list>
    </property>
</bean>

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="properties" ref="projectProperties" />
</bean>

<bean id="uniqueAssetIdRetriever" class="com.mypackage.Myclass">
    <property name="xml" value="${com.myapp.myproperty1}" />
</bean>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM