通过.properties集中管理spring框架中的路径
来源:互联网 发布:nba2kolsf特训全十数据 编辑:程序博客网 时间:2024/05/20 10:55
最近最项目:由于用到了太多的路径,动态写也不大方便(由于后期肯定要改)
所以刚开始为了测试直接写死了:由于越来越多,将来修改写来也不太好找,就专门做了一个.properties文件如下图:
一:新建一个.properties文件
二:然后就是要在applicationContext.xml中配置.properties的扫描
三:封装javaBean
四:用Junit测试:
五:测试,控制台输出如下:
说明配置文件已经正确加载,可以使用了…….
在网上看到的其他的一些方式:
方法一:
- public static String getValue(String fileNamePath, String key)throws IOException {
- Properties props = new Properties();
- InputStream in = null;
- try {
- in = new FileInputStream(fileNamePath);
- // 如果将in改为下面的方法,必须要将.Properties文件和此class类文件放在同一个包中
- //in = propertiesTools.class.getResourceAsStream(fileNamePath);
- props.load(in);
- String value = props.getProperty(key);
- // 有乱码时要进行重新编码
- // new String(props.getProperty("name").getBytes("ISO-8859-1"), "GBK");
- return value;
- } catch (IOException e) {
- e.printStackTrace();
- return null;
- } finally {
- if (null != in)
- in.close();
- }
- }
方法二:
通过spring配置properties文件
- <bean id="propertyConfigurer"
- class="com.hapishop.util.ProjectDBinfoConfigurer">
- <property name="ignoreResourceNotFound" value="true" />
- <property name="locations">
- <list>
- <value>app.properties</value>
- </list>
- </property>
- </bean>
自定义PropertiesConfigurer
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Properties;
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
- import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
- public class PorpertiesConfigurer extends PropertyPlaceholderConfigurer {
- private static Map ctxPropertiesMap;
- @Override
- protected void processProperties(
- ConfigurableListableBeanFactory beanFactoryToProcess,
- Properties props) throws BeansException {
- super.processProperties(beanFactoryToProcess, props);
- ctxPropertiesMap = new HashMap();
- for (Object key : props.keySet()) {
- String keyStr = key.toString();
- String value = props.getProperty(keyStr);
- ctxPropertiesMap.put(keyStr, value);
- }
- }
- public static Object getContextProperty(String name) {
- return ctxPropertiesMap.get(name);
- }
- }
0 0
- 通过.properties集中管理spring框架中的路径
- 通过Spring Session实现Session集中管理
- 通过Spring Session实现Session集中管理
- 分布式配置集中管理框架Smconf
- 通过spring取properties资源
- 通过Spring读取properties配置文件
- java通过相对路径读取properties数据
- 通过Spring读取properties配置文件的信息 Spring 读取properties
- ApplicationContextAware获取Spring上下文,通过基类获取properties中的配置属性
- Spring 通过注解注入Properties文件
- Spring+Hibernage通过jdbc.properties配置
- Spring通过.properties文件引入属性配置
- 通过spring读取properties文件内容
- Java中通过Spring读取Properties文件
- 通过Spring获取properties文件属性值
- Spring通过注解@Value获取properties配置
- spring中通过properties文件配置数据源
- Spring 是怎么管理properties文件的
- 相关有用网址
- Android内核开发:系统启动速度优化
- Linux源码安装mysql 5.5.13 (cmake编译)
- 杭电1062 之 Text Reverse
- nyoj_116士兵杀敌(二)(树状数组)
- 通过.properties集中管理spring框架中的路径
- java NIO之HelloWorld
- 10个值得推荐的免费设计模板网站
- CSS选择器(二)
- MySql时间格式转换
- Android网络请求框架-----Volley
- Android高效内存:让图片占用尽可能少的内存
- Android内核开发:源码的版本与分支详解
- 【Java】static方法与非static方法