【发布时间】:2015-04-21 08:17:30
【问题描述】:
我正在为属性文件苦苦挣扎。在我的应用程序中,我使用两种属性文件方式:在 spring 中和直接在我的应用程序中。
在应用程序中我使用一种方法来获取属性:
private String getHome() {
String name = null;
try {
Properties prop = new Properties();
String propFname = "path.properties";
InputStream is = getClass().getClassLoader().getResourceAsStream(propFname);
if (is == null) {
throw new FileNotFoundException("path.properties File Not found");
}
prop.load(is);
name = prop.getProperty("HOME");
} catch (FileNotFoundException e) {
log.error(e);
System.exit(1);
} catch (IOException e) {
log.error(e);
System.exit(1);
}
return name;
}
然后在我的课堂上我使用:
private static String home = new MailService().getHome();
但对于 JDBC 连接等,我使用带有 context:property-placeholder 的 Spring xml 文件。
我想让这两种方式只有一种——属性文件的位置也不同,从上面我把它们放在 /WEB-INF/classes/ 和 Spring 只放在 /WEB-INF/ 这可能会让人困惑.
关于我的问题:
- 统一这两种方式有什么意义吗,我的意思是它们都是 有它们的含义,还是两者都使用spring更好 他们我已经在使用它了吗?
- 在 Spring 中我还找到了两种初始化属性的方法,一种 使用@Value 注释,其次使用@Autowireing bean。如果我是 正确他们都应该做同样的功能,只是更新 和较旧的风格,还是这两者之间有真正的区别?
- 最后一个问题:我搜索的是保存属性文件的不同方法。最常见的是 /resources、/WEB-INF 和 /classes/config/。它也随着时间的推移而有所不同,还是仅仅取决于程序员他更喜欢什么?我没有找到直接的答案。
对不起,如果这个问题看起来很愚蠢,但是通过搜索不同的答案,我对正确的方法感到困惑。
谢谢。
【问题讨论】:
标签: java spring spring-mvc properties