spring PropertiesLoaderUtils操作Properties文件

本文介绍了如何在Java中加载并获取属性文件的值,包括使用Properties类进行操作及异常处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.IOException;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.support.PropertiesLoaderUtils;

public class PropertiesUtil {

private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);

private static Properties properties= new Properties();

/*properties文件名*/
private static final String PROPERTIES_FILE_NAME="centrin-dao.properties";


public static String getProperty(String name) {
String result = null;

try {
//下载文件
properties = PropertiesLoaderUtils.loadAllProperties(PROPERTIES_FILE_NAME);
result = properties.getProperty(name);//根据name得到对应的value
} catch (IOException e) {
logger.warn(" loadAllProperties error 1");
e.printStackTrace();
}
return result;

}

public static String getProperty(String name ,String properties_name) {
String result = null;

try {
//下载文件
properties = PropertiesLoaderUtils.loadAllProperties(properties_name);
result = properties.getProperty(name);//根据name得到对应的value
} catch (IOException e) {
logger.warn(" loadAllProperties error 1");
e.printStackTrace();
}
return result;

}
public static void main(String[] args) throws IOException {


System.out.println(PropertiesUtil.getProperty("createPlanPath"));

}
}
<think>嗯,用户想知道如何在Spring Boot中将key-value格式的文件加载为Properties对象。首先,我得回忆一下Spring Boot处理配置的机制。记得Spring Boot默认会加载application.properties或者application.yml,这些配置会被存储在Environment中,用户可以通过@Value或者@ConfigurationProperties来获取值。但这里用户的需求是加载自定义的key-value文件,比如不是默认的application文件,而是其他名称的.properties文件。 首先,可能需要使用@PropertySource注解来指定自定义的配置文件。比如,如果用户有一个叫custom.properties文件,放在src/main/resources下,那么可以在配置类上加上@PropertySource("classpath:custom.properties")。不过要注意,这个注解默认支持properties文件,所以不需要额外处理。然后,通过Environment对象或者@Value来获取属性值。 另外,如果用户想手动加载Properties对象,可能需要用Resource和PropertiesLoaderUtils。比如,通过ClassPathResource来获取文件资源,然后用PropertiesLoaderUtils.loadProperties()方法加载。这种方法适用于需要更灵活控制的场景,比如在非Spring管理的类中加载配置。 还要考虑配置文件的编码问题,确保文件是UTF-8格式,避免乱码。同时,如果配置文件不在classpath下,可能需要使用file:路径来指定,比如@PropertySource("file:/path/to/file.properties"),但这种情况较少见,大部分情况还是放在resources目录下。 用户可能遇到的问题包括配置文件位置不正确、文件名拼写错误,或者属性键名称不匹配。需要提醒用户检查这些点。此外,如果使用@ConfigurationProperties,需要确保前缀正确,并且有对应的setter方法。 还有,如果用户想从命令行参数加载配置,可以用ApplicationArguments,但那是另一个话题了。这里应该专注于文件Properties对象的加载。 总结一下,步骤应该是:1. 使用@PropertySource指定自定义文件,2. 注入Environment或使用@Value获取值,3. 或者手动用Resource加载。同时提供示例代码,比如配置类如何写,以及手动加载的方法。最后,注意事项和常见问题,帮助用户避免踩坑。</think>在Spring Boot中读取key-value格式文件Properties对象可以通过以下三种方式实现,根据不同的使用场景选择合适的方法: ### 一、使用@PropertySource注解(推荐) 1. 创建自定义配置文件 在`src/main/resources`目录下创建`custom.properties`文件,内容示例: ``` app.name=MyApplication app.version=1.0.0 ``` 2. 创建配置类 ```java @Configuration @PropertySource("classpath:custom.properties") @ConfigurationProperties(prefix = "app") public class AppConfig { private String name; private String version; // 必须提供setter方法 public void setName(String name) { this.name = name; } public void setVersion(String version) { this.version = version; } // getter方法 } ``` 3. 使用配置值 ```java @Autowired private AppConfig appConfig; public void showConfig() { System.out.println(appConfig.getName()); // 输出:MyApplication } ``` ### 二、手动加载Properties对象 适用于需要直接操作Properties对象的场景: ```java import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.support.PropertiesLoaderUtils; public Properties loadCustomProperties() throws IOException { ClassPathResource resource = new ClassPathResource("custom.properties"); return PropertiesLoaderUtils.loadProperties(resource); } ``` ### 三、通过Environment接口访问 ```java @Autowired private Environment env; public void getProperty() { String appName = env.getProperty("app.name"); String appVersion = env.getProperty("app.version"); } ``` ### 注意事项 1. 文件路径验证 - 确保文件位于classpath下(通常为`src/main/resources`) - 路径写法示例: ```java @PropertySource("classpath:config/custom.properties") // 子目录 @PropertySource("file:/opt/config/custom.properties") // 绝对路径 ``` 2. 多文件加载支持 ```java @PropertySource({"classpath:default.properties", "classpath:override.properties"}) ``` 3. 编码问题处理 ```java @PropertySource(value = "classpath:chinese.properties", encoding = "UTF-8") ``` [^1]: Spring Boot默认加载application.properties的机制 [^2]: 配置文件加载到Environment的实现原理 [^3]: @ConfigurationProperties与@Value的区别
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值