spring 集成的项目,Properties配置文件外移
来源:互联网 发布:学德语的软件 编辑:程序博客网 时间:2024/06/03 22:40
前段时间做了一个项目,在开发的过程中,也没有考虑到配置文件的问题。后来项目完成了,打包的时候要求,要求将项目中的配置文件外移,方便修改配置文件。花了我两天多的时间才弄明白,于是记录下,以防以后再遇到类似问题。
配置文件位于classpath下
使用spring的org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类加载Properties配置文件,通过源码可以知道,默认加载的是classpath下的文件,配置如下:
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:config/init.properties</value> </property> </bean>
如果有多个配置文件加载,则:
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:config/init.properties</value> <value>classpath:config/init.properties</value> </list> </property> </bean>
这样spring就能够加载properties文件了。
配置文件位于外部目录
但是对于外部目录的配置文件,使用org.springframework.beans.factory.config.PropertyPlaceholderConfigurer也是可以加载的,不过要修改他的路径配置方式,如下:
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>file:${user.dir}/config/init.properties</value> <value>file:${user.dir}/config/init2.properties</value> </list> </property> </bean>
这样就可以成功加载外部目录的配置文件了,${user.dir}是系统变量,指用户当前目录所在。
代码中加载配置文件
应该某些需求,配置文件得从java代码是加载,这里我就说一样代码中加载外部目录的配置文件的方式,加载classpath目录下的配置文件这里就不再多说了,相
信网上有太多较好的简答。如下代码:
private static final Properties sysConfig = new Properties(); static { try { InputStream iStream = new FileInputStream(new File("config", "shellConfig.properties")); sysConfig.load(iStream); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static String getPropertyValue(String key){ return sysConfig.getProperty(key); }
希望对大家有所帮助!
- spring 集成的项目,Properties配置文件外移
- Spring的PropertyPlaceholderConfigurer应用与集成项目properties文件管理
- 通过Spring读取properties配置文件的信息 Spring 读取properties
- spring集成dubbo的配置文件
- 使用Properties类读取项目外的配置文件
- Spring 引入properties配置文件的方式
- Spring中使用外部的properties配置文件
- spring注入properties类型的配置文件信息
- 解决eclipse创建spring boot项目加载不到application.properties配置文件的问题
- spring .properties 配置文件
- Spring 使用Properties配置文件
- spring 读取properties配置文件
- spring加载properties配置文件
- spring 配置文件datasource.properties
- spring 配置文件引入properties
- Spring读取properties配置文件
- Struts+spring+hibernate集成的配置文件
- activeMQ和spring集成的配置文件讲解
- 关于优化内存 提高软件运行效率的 写的真的很不错
- Spring JdbcTemplate小结
- 内存优化:SoftReference 和 WeakReference 软引用与弱引用
- 5款主流NoSQL数据库全方位横评
- 使用意图在Activity之间传递数据小插曲__传递对象
- spring 集成的项目,Properties配置文件外移
- 20人年的项目2个星期交接,你从哪里入手?
- java基础--->基本数据类型之间的转换
- 代码中配置log4j 参数
- Chromium for android 1 源码获取
- Spring MVC 学习笔记 json格式的输入和输出
- 使用ANT打包Android应用
- Ibatis,Spring整合(注解方式注入)
- MySQL 三种关联查询的方式: ON vs USING vs 传统风格