gpt4 book ai didi

java - 在 spring 配置中使用属性

转载 作者:搜寻专家 更新时间:2023-11-01 02:31:58 25 4
gpt4 key购买 nike

我有一个属性配置取决于我的环境,像这样:

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

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

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

我试图用第二个 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.properties 仅包含一个属性:my.env=qual

顺序设置为“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)
[..]

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

任何人都可以解释这种行为吗?

最佳答案

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

${my.env:qual}

关于java - 在 spring 配置中使用属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7241356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com