Resource resource = new ClassPathResource("conf/configInfo.properties");app
Properties props = PropertiesLoaderUtils.loadProperties(resource);this
System.out.println(props.get("email.host"));spa
---configInfo.properties配置文件信息.net
email.host = www.94gleaner.comget
email.port = xxxio
email.username = gleanerclass
email.password = xxxemail
email.sendFrom = 94gleaner@94gleaner.com配置
第二種配置文件
@Component("configInfo")
public class ConfigInfo {
@Value("${email.host}")
private String host;
@Value("${email.port}")
private String port;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
}
public class UserController {
@Autowired
private UserService userServiceImpl;
@Autowired
private ConfigInfo configInfo;
@RequestMapping("userLogin")
public void Login(HttpServletRequest re, HttpServletResponse rp) throws IOException {
String name = re.getParameter("name");
String pwd = re.getParameter("pwd");
System.out.println(name + ":" + pwd);
System.out.println(configInfo.getHost());
Resource resource = new ClassPathResource("conf/configInfo.properties");
Properties props = PropertiesLoaderUtils.loadProperties(resource);
System.out.println(props.get("email.host"));
}
<context:property-placeholder location="classpath:conf/*.properties" />