java 在 spring 配置中使用属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7241356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:14:45  来源:igfitidea点击:

Using Properties in spring config

javaspring

提问by echox

I have a property configuration depending on my environment, like this:

根据我的环境,我有一个属性配置,如下所示:

 <bean id="someConfigurer" 
       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">  
        <list> 
            <value>classpath:properties/database.${my.env}.properties</value>
            <value>classpath:properties/morestuff.${my.env}.properties</value>
            [..]
        </list>  
    </property>
</bean>

Now I can keep different property files in my project, for example database.prod.propertiesor database.qual.properties. This works fine, if I start my application with -Dmy.env=foobar.

现在我可以在我的项目中保留不同的属性文件,例如database.prod.propertiesdatabase.qual.properties。这很好用,如果我用 -Dmy.env=foobar 启动我的应用程序。

What happens if no environment is supplied? The application won't start, because of a FileNotFoundException thrown by the PropertyPlaceholderConfigurer. I don't want to keep a copy of all property files as a fallback. What I want is to set a environment as fallback, if the system property is not set.

如果没有提供环境会发生什么?由于 PropertyPlaceholderConfigurer 抛出 FileNotFoundException,应用程序将无法启动。我不想保留所有属性文件的副本作为后备。如果未设置系统属性,我想要的是将环境设置为后备。

I tried to solve this with a second PropertyPlaceholderConfigurer like:

我试图用第二个 PropertyPlaceholderConfigurer 来解决这个问题,比如:

<bean id="fallbackPropertyConfigurer"  
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">  
        <list>  
            <value>classpath:properties/default-env.properties</value>
        </list>  
    </property>
    <property name="order" value="0" />
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
</bean>   

default-env.propertiesconsists of just one property: my.env=qual.

default-env.properties仅包含一个属性:my.env=qual.

The order is set to '0' to be sure, that this bean is evaluated first. I still get the following exception:

order 设置为“0”以确保首先评估此 bean。我仍然收到以下异常:

DEBUG  o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'my.env' in [systemProperties]
DEBUG  o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'my.env' in [systemEnvironment]
DEBUG  o.s.c.e.PropertySourcesPropertyResolver - Could not find key 'my.env' in any property source. Returning [null]
DEBUG  o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'someConfigurer'
INFO   o.s.b.f.c.PropertyPlaceholderConfigurer - Loading properties file from class path resource [properties/default-env.properties]
INFO   o.s.b.f.c.PropertyPlaceholderConfigurer - Loading properties file from class path resource [properties/database.${my.env}.properties]
INFO   o.s.b.f.s.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@435a3a: defining beans [fallbackPropertyConfigurer,someConfigurer,[..],org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [properties/database.${my.env}.properties] cannot be opened because it does not exist
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:87)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681)
    [..]

If I comment out the PropertyPlaceholderConfigurer to get rid of the errors I can use %{my.env}in other beans.

如果我注释掉 PropertyPlaceholderConfigurer 以消除我可以%{my.env}在其他 bean 中使用的错误。

Can anybody explain this behavior?

有人可以解释这种行为吗?

采纳答案by FrVaBe

You can set your default value (in Spring 3) like this:

您可以像这样设置默认值(在 Spring 3 中):

${my.env:qual}

回答by Adriaan Koster

I suggest to localize your build using the Maven Assembly plugininstead of fiddling with filenames in your Spring context.

我建议使用Maven Assembly 插件本地化您的构建,而不是在 Spring 上下文中摆弄文件名。

回答by ragav k

as farmor said, the property 'ignoreUnresolvablePlaceholders' should be set with true for the 1st bean too, as explained in API..

正如farmor所说,第一个bean的属性“ignoreUnresolvablePlaceholders”也应该设置为true,如API中所述。

Default is "false": An exception will be thrown if a placeholder fails to resolve. Switch this flag to "true" in order to preserve the placeholder String as-is in such a case, leaving it up to other placeholder configurers to resolve it.

默认为“false”:如果占位符无法解析,则会抛出异常。将此标志切换为“true”,以便在这种情况下按原样保留占位符字符串,将其留给其他占位符配置器来解决。

回答by Fareed Alnamrouti

you can also use:

您还可以使用:

<property name="ignoreResourceNotFound" value="true"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>

回答by Farmor

A longshoot try to add

一个长镜头尝试添加

property name="ignoreUnresolvablePlaceholders" value="true"/>

属性名称="ignoreUnresolvablePlaceholders" value="true"/>

to your first bean as well

还有你的第一个豆子