Spring 我可以使用PropertyResourceConfigurer';s setOrder方法重写另一个PropertyResourceConfigurator';谁的财产?
我了解Spring 我可以使用PropertyResourceConfigurer';s setOrder方法重写另一个PropertyResourceConfigurator';谁的财产?,spring,Spring,我了解org.springframework.beans.factory.config.propertyResourceConfiger实现org.springframework.core.Ordered。有关setOrder方法,请参阅 但是,我不知道如何使用setOrder方法来处理propertyresourceconfig 我是否可以对一个属性资源配置器使用setOrder覆盖另一个属性资源配置器的属性?如果setOrder不打算以这种方式使用,那么有人能在propertyresourc
org.springframework.beans.factory.config.propertyResourceConfiger
实现org.springframework.core.Ordered
。有关setOrder
方法,请参阅
但是,我不知道如何使用setOrder
方法来处理propertyresourceconfig
我是否可以对一个属性资源配置器使用setOrder
覆盖另一个属性资源配置器的属性?如果setOrder
不打算以这种方式使用,那么有人能在propertyresourceconfig
的上下文中给我一个这种方法的实际用例吗
编辑1:以下是我的属性资源占位符配置:
@Configuration
public class PropertyConfigurerConfiguration {
static class defaultConfiguration {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws IOException {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(Boolean.TRUE);
propertySourcesPlaceholderConfigurer.setOrder(35);
propertySourcesPlaceholderConfigurer.setLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:META-INF/props/*.properties"));
return propertySourcesPlaceholderConfigurer;
}
}
@Profile(Profiles.DEV)
static class devConfiguration {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws IOException {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(Boolean.TRUE);
propertySourcesPlaceholderConfigurer.setOrder(25);
propertySourcesPlaceholderConfigurer.setLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:META-INF/props/dev/*.properties"));
return propertySourcesPlaceholderConfigurer;
}
...
下面是激活配置文件的方法:spring.profiles.active=“dev”
编辑2:
在阅读您关于bean ID的一条评论时,我对代码进行了如下修改:
@Profile(Profiles.DEV)
static class devConfiguration {
@Bean
public static PropertySourcesPlaceholderConfigurer devPropertySourcesPlaceholderConfigurer() throws IOException {//Now called devProperXXX
现在看来它可以工作了
有人能给我解释一下为什么改变bean id会改变行为吗这是在某处记录的吗?如果是在哪里?希望我能正确理解这一点。简短的回答是“是”。
如果有两个bean定义为
<bean id="mappings"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties">
<value>
p1=v1
p2=v2
</value>
</property>
<property name="order" value="25" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
<bean id="mappings2"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties">
<value>
p1=v111
p3=v3
</value>
</property>
<property name="order" value="35" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
p1=v1
p2=v2
p1=v111
p3=v3
“p1”属性的值将是“v1”,因为其属性PlaceHolderConfigure
的顺序较低(因此优先级较高)
我认为这一点很有用的一个场景是.jar,它有自己的配置程序和自己的.properties
文件。如果在这些JAR中搜索.xml配置文件(在classpath*:*.xml
一种样式中),我认为可以通过定义另一个具有较低顺序值的PropertyPlaceHolderConfigure(默认顺序为Integer.MAX\u value
)来覆盖其中一个xml文件中的属性.您是指抽象类PropertyResourceConfigure
或其具体实现之一,如PropertyPlaceHolderConfigure
?此外,我不认为您仅通过使用有序就可以覆盖PropertyResourceConfigure
(或具体实现)的一个实例Ordered
用于设置应用后处理器列表的顺序,如果您希望后处理器列表按特定顺序应用。如果您有两个PropertyPlaceHolderConfigurer
,它们将按您指定的顺序应用,但两者将共存。谢谢。我实际上是说,一个配置程序的属性将覆盖另一个配置程序的属性。这就是我的问题所指的…我已经编辑了我的问题。非常感谢,我将尝试你的建议,并在这里作出相应的答复。我已经按照建议进行了尝试。不幸的是,属性没有按照要求“合并”。。。因此,您要么得到p1和p2(如果选择了映射),要么得到p1和p3(如果选择了映射S2)…:-(您的场景是如何配置的?第一个PropertyPlaceHolderConfigure在哪里,第二个在哪里?另外,这两个配置器有什么bean ID?当您有两个具有相同ID的bean时,它被覆盖,一个bean定义正在替换另一个。使用不同的ID,您将得到两个都已加载且其属性为m的bean。)在重复属性的情况下,顺序至关重要。