Spring中config属性文件的读取与使用 PropertyPlaceholderConfigurer 注解@Component
1.配置文件:/WEB-INF/configInfo.properties配置文件内容:email.host = www.94gleaner.comemail.port = xxxemail.username = gleaneremail.password = xxxemail.sendFrom = 94gleaner@94gleaner.com
2.Spring容器启动时,使用内置bean对属性文件信息进行加载,在bean.xml中添加如下:
/WEB-INF/configInfo.properties
3.其它bean中引用,例:
${email.host}
${email.port}
4.JAVA代码中获取方法:创建ConfigInfo.java@Component(“configInfo”)public class ConfigInfo {
@Value(“${email.host}”)
private String host;
@Value(“${email.port}”)
private String port;
public String getHost() {
return host;
}
public String getPort() {
return port;
}
}
5.代码中调用处@Autowiredprivate ConfigInfo configInfo;public byte[] Test(HttpServletResponse response) {
string host = configInfo.getHost();
}