我创建了这个测试应用程序来解释我遇到的问题。
我有两个具有相同键的属性文件,并且在类路径中共有三个属性文件。
1.mysql.properties
2.vertica.properties
3.other.properties
运行该应用程序时出现以下错误。
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'mysql-username' in value "${mysql-username}"
这是代码。
package com.test.demo;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
@Configuration
@ComponentScan
public class DemoApplication {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(DemoApplication.class);
MyController bean = applicationContext.getBean(MyController.class);
bean.log();
}
@Bean
public PropertyPlaceholderConfigurer otherProps() {
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setIgnoreResourceNotFound(true);
final List<Resource> resourceList = new ArrayList<>();
resourceList.add(getOtherResourceFile());
ppc.setLocations(resourceList.toArray(new Resource[]{}));
return ppc;
}
@Bean
public PropertyPlaceholderConfigurer verticaProps() {
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setIgnoreResourceNotFound(true);
ppc.setPlaceholderPrefix("${vertica-");
final List<Resource> resourceList = new ArrayList<>();
resourceList.add(getVerticaResourceFile());
ppc.setLocations(resourceList.toArray(new Resource[]{}));
return ppc;
}
@Bean
public PropertyPlaceholderConfigurer mysqlProps() {
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setIgnoreResourceNotFound(true);
ppc.setPlaceholderPrefix("${mysql-");
final List<Resource> resourceList = new ArrayList<>();
resourceList.add(getDatabasePropertiesFile());
ppc.setLocations(resourceList.toArray(new Resource[]{}));
return ppc;
}
private Resource getOtherResourceFile() {
Resource resource = new ClassPathResource("other.properties");
return resource;
}
private Resource getVerticaResourceFile() {
Resource resource = new ClassPathResource("vertica.properties");
return resource;
}
private Resource getDatabasePropertiesFile() {
Resource resource = new ClassPathResource("mysql.properties");
return resource;
}
}
MyController.java
/*
* MyController.java
*/
package com.test.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
*
* @author
*/
@Component
public class MyController {
@Autowired
private MySqlProperties mySqlProperties;
@Autowired
private VerticaProperties verticaProperties;
@Autowired
private OtherProperties otherProperties;
public String log() {
System.out.println(mySqlProperties.getUsername());
System.out.println(verticaProperties.getUsername());
System.out.println(otherProperties.getKey());
return "hello";
}
}
MySqlProperties.java
/*
* MySqlProperties.java
*/
package com.test.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
*
* @author
*/
@Component
public class MySqlProperties {
@Value("${mysql-username}")
private String username;
@Value("${mysql-password}")
private String password;
//getter
}
VerticaProperties.java
/*
* VerticaProperties.java
*/
package com.test.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
*
* @author
*/
@Component
public class VerticaProperties {
@Value("${vertica-username}")
private String username;
@Value("${vertica-password}")
private String password;
//getter
}
OtherProperties.java
/*
* OtherProperties.java
*/
package com.test.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
*
* @author
*/
@Component
public class OtherProperties {
@Value("${key}")
private String key;
@Value("${key1}")
private String key1;
//getter
}
这些是属性文件。
mysql.properties
username=mysql
password=mysql
vertica.properties
username=vertica
password=vertica
other.properties
key=value
key1=value1
奇怪的问题::当我从DemoApplication.java中删除以下代码时,该应用程序可以正常工作。
@Bean
public PropertyPlaceholderConfigurer otherProps() {
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setIgnoreResourceNotFound(true);
final List<Resource> resourceList = new ArrayList<>();
resourceList.add(getOtherResourceFile());
ppc.setLocations(resourceList.toArray(new Resource[]{}));
return ppc;
}