[英]Individual spring "beans" properties value overridden while setting with corresponding dedicated "properties" file (using @PropertySource)
I am trying to initialize two different spring beans (bean1 and bean2) with their dedicated properties files ('bean1' from 'bean1.properties', and 'bean2' from bean2.properties), with both bean1 and 2 properties file having same "enter code here'eys" with a different value.我试图用它们的专用属性文件('bean1.properties' 中的'bean1' 和 bean2.properties 中的'bean2')初始化两个不同的 spring beans(bean1 和 bean2),bean1 和 2 属性文件具有相同的“在此处输入代码'eys”,具有不同的值。 But by attempting to do so, both bean1 and bean2 are getting initialize by only "values" from bean1.properties (while bean2.properties is ignored).
但是通过尝试这样做, bean1 和 bean2 都仅通过 bean1.properties 的“值”进行初始化(而 bean2.properties 被忽略)。
The demo code is on GitHub演示代码在GitHub 上
Basically used @PropertySource to load corresponding property file from classpath.基本上使用@PropertySource 从类路径加载相应的属性文件。
@Component
@PropertySource("classpath:bean1.properties")
@ConfigurationProperties
public class Bean1 {
private String symbol;
private String tenor;
// omitting code
}
@Component
@PropertySource("classpath:bean2.properties")
@ConfigurationProperties
public class Bean2 {
private String symbol;
private String tenor;
// omitting other code
}
bean1.properties: bean1.properties:
symbol=bean1symbol
tenor=bean1tenor
bean2.properties bean2.properties
symbol=bean2symbol
tenor=bean2tenor
I expect the bean1 and bean2 properties initialized based on corresponding values of their properties files, [when they same same key].我希望 bean1 和 bean2 属性根据其属性文件的相应值进行初始化,[当它们具有相同的键时]。
When I print symbol and tenor for Bean1 and Bean2, symbol and tenor are printing same values (from bean2.properties).当我为 Bean1 和 Bean2 打印符号和男高音时,符号和男高音打印相同的值(来自 bean2.properties)。
The problem is with property name collision in the Spring Environment
.问题在于 Spring
Environment
中的属性名称冲突。 You are using @PropertySource
to tell Spring to source properties from additional location(s), but those properties are going into the same Environment
.您正在使用
@PropertySource
告诉 Spring 从其他位置获取属性,但这些属性将进入相同的Environment
。
Instead, try prefixing your properties within the *.properties files and using the @ConfigurationProperties(prefix = "my.prefix")
to disambiguate properties with the same name.相反,尝试在 *.properties 文件中为您的属性添加前缀,并使用
@ConfigurationProperties(prefix = "my.prefix")
消除具有相同名称的属性的歧义。
For example:例如:
@Component
@PropertySource("classpath:bean1.properties")
@ConfigurationProperties(prefix = "bean1")
@Configuration
public class Bean1 {
private String symbol;
private String tenor;
//omitting code
}
@Component
@PropertySource("classpath:bean2.properties")
@ConfigurationProperties(prefix = "bean2")
@Configuration
public class Bean2 {
private String symbol;
private String tenor;
//omitting other code
}
Then in your *.properties files you would have:然后在您的 *.properties 文件中,您将拥有:
bean1.symbol=
bean1.tenor=
bean2.symbol=
bean2.tenor=
Spring will read all properties files into the same namespace - so for the case that bean2.properties will be read after bean1.properties it will overwrite the already defined properties. Spring 会将所有属性文件读入同一个命名空间 - 因此对于 bean2.properties 将在 bean1.properties 之后读取的情况,它将覆盖已经定义的属性。
So maybe you could modify your properties to be named like this:所以也许你可以修改你的属性以这样命名:
bean1.symbol=bean1symbol
bean2.tenor=bean1tenor
bean2.symbol=bean2symbol
bean2.tenor=bean2tenor
or simpler:或更简单:
symbol1=bean1symbol
tenor2=bean1tenor
symbol2=bean2symbol
tenor2=bean2tenor
So please be carefully that your properties name a globally unique.所以请注意您的属性名称是全局唯一的。
For already existing properties please have a look at Common application properties对于已经存在的属性,请查看通用应用程序属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.