英文:
Spring Framework apllication-profile.properties doesn't set property value
问题
这是我的application.properties文件
spring.profiles.active=de
username=postgres
password=12345
dburl=localhost:postgre
这是application-de.properties文件
username=**** postgres
password=****12345
dburl=localhost:****de
这是@Configuration类
@Value("${username}")
String username;
@Value("${password}")
String password;
@Value("${dburl}")
String dburl;
@Bean
public static PropertySourcesPlaceholderConfigurer property() {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
return propertySourcesPlaceholderConfigurer;
}
@Bean
FakeDataSource fakeDataSource() {
FakeDataSource fakeDataSource = new FakeDataSource();
fakeDataSource.setUsername(username);
fakeDataSource.setPassword(password);
fakeDataSource.setDburl(dburl);
return fakeDataSource;
}
这是主类
@SpringBootApplication
public class Spring5DiDemoDiAssignmentApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Spring5DiDemoDiAssignmentApplication.class, args);
FakeDataSource fakeDataSource = (FakeDataSource) ctx.getBean(FakeDataSource.class);
System.out.println(fakeDataSource.getUsername() + " " + fakeDataSource.getPassword() + " " + fakeDataSource.getDburl());
这是输出
User ****12345 localhost:****de
所有属性都已设置,但用户名未设置。如果我将apllication-de.properties更改如下
name=**** postgres
password=****12345
dburl=localhost:****de
和@Configuration类
@Value("${name}")
String username;
输出将为
**** postgres ****12345 localhost:****de
英文:
This is my application.properties file
spring.profiles.active=de
username=postgres
password=12345
dburl=localhost:postgre
This is application-de.properties file
username=**** postgres
password=****12345
dburl=localhost:****de
This is @Configuration class
@Value("${username}")
String username;
@Value("${password}")
String password;
@Value("${dburl}")
String dburl;
@Bean
public static PropertySourcesPlaceholderConfigurer property(){
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new
PropertySourcesPlaceholderConfigurer();
return propertySourcesPlaceholderConfigurer;
}
@Bean
FakeDataSource fakeDataSource(){
FakeDataSource fakeDataSource = new FakeDataSource();
fakeDataSource.setUsername(username);
fakeDataSource.setPassword(password);
fakeDataSource.setDburl(dburl);
return fakeDataSource;
}
This is main class
@SpringBootApplication
public class Spring5DiDemoDiAssignmentApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Spring5DiDemoDiAssignmentApplication.class,
args);
FakeDataSource fakeDataSource = (FakeDataSource) ctx.getBean(FakeDataSource.class);
System.out.println(fakeDataSource.getUsername()+" "+fakeDataSource.getPassword()+"
"+fakeDataSource.getDburl());
This is output
User ****12345 localhost:****de
All properties set but username doesn't set. If I change apllication-de.properties as below
name=**** postgres
password=****12345
dburl=localhost:****de
and @Configuration class
@Value("${name}")
String username;
The output
**** postgres ****12345 localhost:****de
答案1
得分: 0
username
属性已经在您的environmentProperties
中默认定义,并由PropertySourcesPropertyResolver
解析,这就是您无法注入所需值的原因。
为了避免这种问题,通常的做法是在您的自定义属性中添加前缀。
因此,您的application.properties
和application-de.properties
应该包含类似以下的内容:
app.username=**** postgres
app.password=****12345
app.dburl=localhost:****de
然后,您的@Configuration
文件应该包含:
@Value("${app.username}")
String username;
@Value("${app.password}")
String password;
@Value("${app.dburl}")
String dburl;
这样,您应该避免与任何默认属性发生冲突。
请注意,app
前缀仅是一个示例,您可以用任何您喜欢的名称替换它,如果您愿意,还可以链接前缀,例如my.app.prop.username
。
英文:
The username
property is already defined by default in your environmentProperties
and is being resolved by the PropertySourcesPropertyResolver
, so that's why you are not able to inject the value that you need.
In order to avoid such issues, the common practice is to add the prefix to your custom properties.
So, your application.properties
and application-de.properties
should contain something like
app.username=**** postgres
app.password=****12345
app.dburl=localhost:****de
and then your @Configuration
file should look contain:
@Value("${app.username}")
String username;
@Value("${app.password}")
String password;
@Value("${app.dburl}")
String dburl;
This way you should avoid collisions with any default properties.
Be aware that app
prefix is only an example and you can replace it with whatever name you wish, you can also chain prefixes like my.app.prop.username
if you wish.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论