1、通过spring配置properties文件
[java]
<bean id="propertyConfigurer"
class="comtjsoftbaseutilCustomizedPropertyPlaceholderConfigurer">
/WEB-INF/config/jdbcproperties
/WEB-INF/config/mailproperties
/WEB-INF/config/systemproperties
其中class为自己定义的类
public class Config {
//Properties继承于HashMap key:value都是String类型
private Properties table=new Properties();
public Config(String file){
try{
tableload(new FileInputStream(file));
}catch (IOException e){
eprintStackTrace();
throw new RuntimeException(e);
}
}
public int getInt(String key){
return IntegerparseInt(tablegetProperty(key));
}
public String getString(String key){
return tablegetProperty(key);
}
public double getDouble(String key){
return DoubleparseDouble(tablegetProperty(key));
}
}
说说我的项目中的情况吧:
配置文件“weblogic11gproperties”保存在WEB-INFO目录下,和webxml在同一个目录下。
一个JavaBean专门用于读取配置文件的内容:
public class PropertiesIO {
private String fileName = null;
public PropertiesIO(String fileName){
thisfileName = getClass()getClassLoader()getResource("/")getPath() + "\\" + fileName;
}
public String getValue(String key){
try{
InputStream in = new FileInputStream(fileName);
Properties prop = new Properties();
propload(in);
inclose();
return propgetProperty(key);
}
catch(Exception err){
errprintStackTrace();
return null;
}
}
}
重点说明:getClass()getClassLoader()getResource("/")会得到当前项目下的“WEB-INF\classes”目录,即JavaBean的class文件的根目录,
getClass()getClassLoader()getResource("/")getPath() + "\\" + fileName
就会得到当前项目下的“WEB-INF\weblogic11gproperties”文件。
getValue()是根据键值得到相应配置项的内容,这样就简单了。
以上就是关于Java中怎么获取spring中配置的properties属性文件内容全部的内容,包括:Java中怎么获取spring中配置的properties属性文件内容、页面如何获取properties文件中的值、java读取properties配置文件路径问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)