spring 使用代码如何读取properties配置文件
直接使用spirng程序代码读取项目的配置文件方法
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.core.io.FileSystemResource;
public class Test {
/**
* @param args
*/
public static void main( String[] args ) {
String configFile = "D:/test/application.properties";
//如果配置文件在classpath目录下可以使用ClassPathResource对象
//Resource resource = new ClassPathResource("/application.properties");
Resource resource = new FileSystemResource( configFile );
try {
Properties property = PropertiesLoaderUtils.loadProperties(resource);
String driver = property.getProperty("jdbc.driver");
String url = property.getProperty("jdbc.url");
String userName = property.getProperty("jdbc.username");
String password = property.getProperty("jdbc.password");
}
catch (IOException e1) {
//log.error("read config file failed", e1);
}
}
}
如果配置文件在classpath目录下可以使用ClassPathResource对象
Resource resource = new ClassPathResource("/application.properties");
来源://作者:/更新时间:2014-07-17
顶
踩
相关文章:

