spring DM配置文件加载
2015-09-18 09:18
357 查看
spring DM支持Configuration Admin,可以使配置文件的内容注入到程序当中。
包括以下三种方式:
1. 把配置文件内容转换为properties,可以设置配置项默认值
2.配置项自动关联对象属性
3.配置文件修改后动态加载
指定update-method方法,update-strategy为“bean-managed”
在karaf容器中运行时,以persistent-id为配置文件名,后缀为”.cfg”,放入etc目录中即可
参考:http://docs.spring.io/osgi/docs/current/reference/html/compendium.html
包括以下三种方式:
1. 把配置文件内容转换为properties,可以设置配置项默认值
<!-- Configuration Admin entry --> <osgix:cm-properties id="cmProps" persistent-id="com.xyz.myapp"> <prop key="host">localhost</prop> </osgix:cm-properties> <!-- placeholder configurer --> <ctx:property-placeholder properties-ref="cmProps" /> <bean id="dataSource" ...> <property name="host" value="${host}"/> <property name="timeout" value="${timeout}"/> </bean>
2.配置项自动关联对象属性
public class MessageTank { private int amount; public int getAmount() { return this.amount; } public void setAmount(int amount) { this.amount = amount; } } <bean id="managedComponent" class="MessageTank"> <osgix:managed-properties persistent-id="com.xyz.messageservice"/> </bean>
3.配置文件修改后动态加载
<bean id="zhErrorMsgLoader" class="com.sq.platform.webservices.utils.ZhErrorMsgLoaderUtil"> <osgix:managed-properties persistent-id="error_zh_CN" update-strategy="bean-managed" update-method="updateProperties"/> </bean>
指定update-method方法,update-strategy为“bean-managed”
public class ZhErrorMsgLoaderUtil { @Autowired private ErrorMsgFormatter errorMsgFormatter; public void updateProperties(Map<String, ?> properties) throws UnsupportedEncodingException { Map<String, String> zhProperties = new HashMap<>(); for (Map.Entry entry : properties.entrySet()) { zhProperties.put((String) entry.getKey(), new String(entry.getValue().toString().getBytes("ISO-8859-1"), "UTF-8")); } errorMsgFormatter.setCnMsg(zhProperties); } }
在karaf容器中运行时,以persistent-id为配置文件名,后缀为”.cfg”,放入etc目录中即可
参考:http://docs.spring.io/osgi/docs/current/reference/html/compendium.html
相关文章推荐
- OSGI实战教程【Spring DM】编写一个简单的Bundle组件
- zz J2EE高手眼里的OSGi
- Java(1)--关于jdk文档
- test4.6
- 搜索引擎中长串匹配及聚类算法:后缀树算法(Java版)
- java中的sleep()和wait()的区别
- java-web-j2e学习建议路线
- spring+hibernate删除单条记录的几种方法
- Eclipse背景颜色修改
- MyEclipse 2015优化技巧
- JAVA监听器原理
- Java总结篇系列:Java多线程(二)
- Java入门基础之常规的命名方法和变量的值及其引用
- JAVA 笔记
- Java中abstract和interface的区别
- Java泛型在spring jdbc template中的类似应用
- 微冷的雨Java基础学习手记(一)
- Java反射讲解
- 如何快速定位到Eclipse自动添加的TODO
- java中Collection 与Collections的区别