Spring Framework应用程序配置文件中未设置属性值。

huangapple go评论85阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年6月15日 15:09:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479950.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定