前言:很多时候我们都会在resourc目录下写配置信息,如下。
通常使用@PropertySource()注解就可以。也有其他方式,这里不做演示,感兴趣的朋友可以去试试;
下面是写法:交给spring管理,设置前缀,设置配置文件名称。
这样我们就完成了内部配置文件加载。
/**
* @author Eliauk
* @version 1.0
* @description: TODO
* @date 2022/11/11 13:38
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Configuration
@ConfigurationProperties(prefix = "user")
@PropertySource(value = {"classpath:temp.properties"})
public class User {
private String username;
private String account;
private String password;
}
#temp.properties
user.username=xiaohe
user.account=123
user.password=123
#temp1.properties
user.username=xiaohe
user.account=123
user.password=123
最近项目要求用java写个websocket客户端系统,用来桥接外部系统进行实时行情数据推送。由于只是一个小组件,要求打成jar部署到已有的一套自动化平台上,所以配置文件需要外配。翻spring文档发现可以支持。
写法如下:
/**
* @author Eliauk
* @version 1.0
* @description: TODO
* @date 2022/11/11 13:38
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Configuration
@ConfigurationProperties(prefix = "user")
@PropertySource(value = {"file:./temp.properties"})
public class User {
private String username;
private String account;
private String password;
}
将配置文件temp.properties放在和jar同级目录;即可。
由于系统需要调试和修改,所以我自己本地启动是很频繁的;这就带来一个问题,每次打包我都需要将路径改一下,本地启动的时候又要改回classpath,后面就将二者都配置了进去。
@PropertySource(value = {"classpath:temp.properties","file:./temp.properties"},ignoreResourceNotFound = true)
当我们配置多路径,且多路径下配置文件都存在时,springboot会都加载且会覆盖相同内容。所以当我们配置信息只区分外部和内部路径、内容完全相同时,将file路径写在后面就可以了。当我们本地启动时,因为不存在file路径,所以会加载classpath;当jar启动时,file路径会覆盖classpath路径下的内容;
ignoreResourceNotFound = true 一定要加上,否则找不到会报错。加上之后会忽略找不到的配置文件。
对应加载信息源码附上:
ConfigurationClassParser.class
/**
* Process the given <code>@PropertySource</code> annotation metadata.
* @param propertySource metadata for the <code>@PropertySource</code> annotation found
* @throws IOException if loading a property source failed
*/
private void processPropertySource(AnnotationAttributes propertySource) throws IOException {
String name = propertySource.getString("name");
if (!StringUtils.hasLength(name)) {
name = null;
}
String encoding = propertySource.getString("encoding");
if (!StringUtils.hasLength(encoding)) {
encoding = null;
}
String[] locations = propertySource.getStringArray("value");
Assert.isTrue(locations.length > 0, "At least one @PropertySource(value) location is required");
boolean ignoreResourceNotFound = propertySource.getBoolean("ignoreResourceNotFound");
Class<? extends PropertySourceFactory> factoryClass = propertySource.getClass("factory");
PropertySourceFactory factory = (factoryClass == PropertySourceFactory.class ?
DEFAULT_PROPERTY_SOURCE_FACTORY : BeanUtils.instantiateClass(factoryClass));
for (String location : locations) {
try {
String resolvedLocation = this.environment.resolveRequiredPlaceholders(location);
Resource resource = this.resourceLoader.getResource(resolvedLocation);
addPropertySource(factory.createPropertySource(name, new EncodedResource(resource, encoding)));
}
catch (IllegalArgumentException | FileNotFoundException | UnknownHostException ex) {
// Placeholders not resolvable or resource not found when trying to open it
if (ignoreResourceNotFound) {
if (logger.isInfoEnabled()) {
logger.info("Properties location [" + location + "] not resolvable: " + ex.getMessage());
}
}
else {
throw ex;
}
}
}
}