spring 如何动态加载properties文件的内容
1. 在xml中配置你的properties路径:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list> <!-- 指定资源文件基名称 jdbc为文件名,不包含扩展名 -->
<value>classpath:resource/jdbc</value>
</list>
</property>
</bean>
2. 获取WebApplicationContext(需要入参HttpServletRequest request) ServletContext servletContext = request.getSession() .getServletContext(); WebApplicationContext ctx = WebApplicationContextUtils .getRequiredWebApplicationContext(servletContext);
3. 通过WebApplicationContext获取中键值 String msg = ctx.getMessage("jdbc.url", null, Locale.CHINA);
----------------------------------------------------------------------------------------------------------------
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="parentMessageSource" ref="bizMessageSource"/>
<property name="basenames">
<list>
<value>resources.cls-web-resources</value>
<value>resources.cls-web-resources-definitions</value>
<value>resources.cls-web-resources-menu</value>
</list>
</property>
</bean>
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="parentMessageSource" ref="bizMessageSource"/>
<property name="fallbackToSystemLocale"><value>false</value></property>
<property name="basenames">
<list>
<value>classpath:resources/cls-web-resources</value>
<value>classpath:resources/cls-web-resources-definitions</value>
<value>classpath:resources/cls-web-resources-menu</value>
</list>
</property>
</bean>
1.ResourceBundleMessageSource在xml配置中无法指定编码:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value >error</value >
<value >message</value >
</list>
</property>
</bean>
而ReloadableResourceBundleMessageSource可以指定编码,譬如:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="defaultEncoding" value ="gbk" />
<property name="basename" value ="message" />
</bean>
2.加载资源文件的方式不同:
1).下面看下它们的源代码:
ResourceBundleMessageSource的加载,使用ClassUtils.getDefaultClassLoader()加载器,getDefaultClassLoader的方法代码如下:
p lic static ClassLoader getDefaultClassLoader()
{
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
}
catch (Throwable ex) {
logger.debug("Cannot access thread context ClassLoader - falling back to system class loader", ex);
}
if (cl == null)
{
cl = ClassUtils.class.getClassLoader();
}
return cl;
}
//这种方式也是JVM默认的加载方式,先从当前线程中获取类加载器,如果没有,就获取这个类本身的类加载器
2).ReloadableResourceBundleMessageSource默认也使用ClassUtils.getDefaultClassLoader()加载器,它加载资源的方式如下:
p lic Resource getResource(String location)
{
Assert.notNull(location, "Location must not be null");
if (location.startsWith("classpath:")) {
return new ClassPathResource(location.s string("classpath:".length()), getClassLoader());
}
try
{
URL url = new URL(location);
return new UrlResource(url);
}
catch (MalformedURLException ex)
{
return getResourceByPath(location);
}
}
3). 小结:ResourceBundleMessageSource从classloader中加载资源文件,可以找到,
ReloadableResourceBundleMessageSource加载时,默认使用DefaultResourceLoader,他会先判断资源path是否带有classpath:前缀,如果有,用 ClassPathResource去加载资源文件,如果没有试着用文件协议的url去访问,再没有就在contextPath即WEB-INF下查找。
下面做一个Spring的MessageSource的示例:
1.我们单独新建一个spring消息文件beans-message.xml中加如下配置:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value >error</value>
<value >message</value >
</list>
</property>
</bean>
2.这段配置假定在你的classpath中有两个资源文件(resource bundle),它们是error, message。通过ResourceBundle,使用JDK中解析消息的标准方式,来处理任何解析消息的请求。出于示例的目的,假定 message_zh_CN.properties的资源文件的内容为…
msg.common.serverBusy = \非\常\抱\歉,\系\统\十\分\繁\忙\!
#非常抱歉,系统十分繁忙!
msg.argument.required={0}\是\个\必\填\项\!
#{0}是个必填项!
3.再写一个测试类:
p lic class MessageTest {
p lic static void main(String[] args) {
MessageSource resources = new ClassPathXmlApplicationContext("beans-message.xml");
String message = resources.getMessage("msg.common.serverBusy", null, "Default", null);
System.out.println(message);
String message1 = resources.getMessage("msg.argument.required", new Object[] { "'联系方式'" }, null, Locale.CHINA);
System.out.println(message1);
}
}
结果输入为:
非常抱歉,系统十分繁忙!
'联系方式'是个必填项!
3.在我们的项目中,MessageSource不会单独使用,通常我们会把它和自己的业务一起使用,这时候我们可以直接用它本身的方法,我们也可以在其中加入我们自己的逻辑:如,自定义的一个消息类:
p lic class MessageSourceHelper {
private ResourceBundleMessageSource messageSource;
p lic String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
String msg = messageSource.getMessage(code, args, defaultMessage, locale);
return msg != null ? msg.trim() : msg;
}
p lic void setMessageSource(ResourceBundleMessageSource messageSource) {
this.messageSource = messageSource;
}
}
在beans-message.xml中注入:
<bean id="messageSourceHelper" class="com.myspring.message.MessageSourceHelper">
<property name="messageSource">
<ref local="messageSource" />
</property>
</bean>
4.我们可以在MessageSourceHelper中加入自己的业务,注入依赖后,就可以在其他类中调用MessageSourceHelper中的方法。
5.理论简要:ApplicationContext接口扩展了MessageSource 接口,因而提供了消息处理的功能(i18n或者国际化)。与HierarchicalMessageSource一起使用,它还能够处理嵌套的消息,这些是Spring提供的处理消息的基本接口。让我们快速浏览一下它所定义的方法:
· String getMessage(String code, Object[] args, String default, Locale loc):用来从MessageSource获取消息的基本方法。如果在指定的locale中没有找到消息,则使用默认的消息。args中的参数将使用标准类库中的MessageFormat来作消息中替换值。
· String getMessage(String code, Object[] args, Locale loc):本质上和上一个方法相同,其区别在:没有指定默认值,如果没找到消息,会抛出一个NoS hMessageException异常。
· String getMessage(MessageSourceResolvable resolvable, Locale locale):上面方法中所使用的属性都封装到一个MessageSourceResolvable实现中,而本方法可以指定 MessageSourceResolvable实现。
当一个ApplicationContext被加载时,它会自动在context中查找已定义为MessageSource类型的bean。此bean的名称须为messageSource。如果找到,那么所有对上述方法的调用将被委托给该 bean。否则ApplicationContext会在其父类中查找是否含有同名的bean。如果有,就把它作为MessageSource。如果它最终没有找到任何的消息源,一个空的StaticMessageSource将会被实例化,使它能够接受上述方法的调用。
Spring目前提供了两个MessageSource的实现:ResourceBundleMessageSource和StaticMessageSource。它们都继承 NestingMessageSource以便能够处理嵌套的消息。StaticMessageSource很少被使用,但能以编程的方式向消息源添加消息。ResourceBundleMessageSource会用得更多一些
spring 如何动态加载properties文件的内容的更多相关文章
- Spring -09 -在Spring工程 中加载 properties 文件 -为某个属性添加注解赋初值
1.在src 下新建 xxx.properties 文件,不要任意加空格,注明jdbc等标识名!2.在spring 配置文件中先引入xmlns:context,在下面添加2.1如果需要记载多个配置文件 ...
- spring配置数据源(加载properties文件)
1.在spring中引入properties配置文件需要引入context的命名空间和真实地址 2.然后加载文件 需要注意的是这是采用的是set注入方式,所以name属性值必须是连接池set方法名去掉 ...
- Spring加载properties文件的属性的值
要使用配置文件的值首先在spring.xml配置加载properties文件 <context:property-placeholder location="classpath:ife ...
- 不停服务,动态加载properties资源文件
系统运行过程中,我们用注解@Value("${****}")可以获取资源文件中的内 容,获取的内容会被存储在spring缓存中,因此如果我们修改了资源文件,要 想读取到修改后的内容 ...
- spring入门(二)【加载properties文件】
在开发过程当中需要用到配置信息,这些信息不能进行硬编码,这时配置文件是一个比较好的方式,java提供了properties格式的文件,以键值对的方式保存信息,在读取的时候通过键获得键对应的值,spri ...
- Spring加载properties文件的两种方式
在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可, ...
- Java_Java中动态加载jar文件和class文件
转自:http://blog.csdn.net/mousebaby808/article/details/31788325 概述 诸如tomcat这样的服务器,在启动的时候会加载应用程序中lib目录下 ...
- [转载] Java中动态加载jar文件和class文件
转载自http://blog.csdn.net/mousebaby808/article/details/31788325 概述 诸如tomcat这样的服务器,在启动的时候会加载应用程序中lib目录下 ...
- JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(12):XML配置自动扫描包,自动加载*.properties文件
一.XML和注解组合使用 前几篇的测试案例都是在Java类中配置,现在换一种使用方式,在XML中配置,使Spring IoC容器在启动之后自动去扫描配置的包路径,扫描加载指定路径下的propertie ...
随机推荐
- Postgres远程访问配置
在服务器上安装了Postgres数据库,然后通过客户端工具pgAdminIII来远程访问的过程中发现提醒服务器没有启动监听的错误.解决方法如下: 编辑Postgres安装路径下的/data/pg_hb ...
- URAL 1519 Formula 1 (插头DP,常规)
题意:给一个n*m的矩阵,格子中是'*'则是障碍格子,不允许进入,其他格子都是必走的格子,所走格子形成一条哈密顿回路,问有多少种走法? 思路: 本来是很基础的题,顿时不知道进入了哪个坑.这篇插头DP的 ...
- poj 2411 Mondriaan's Dream (轮廓线DP)
题意:有一个n*m的棋盘,要求用1*2的骨牌来覆盖满它,有多少种方案?(n<12,m<12) 思路: 由于n和m都比较小,可以用轮廓线,就是维护最后边所需要的几个状态,然后进行DP.这里需 ...
- SAP Cloud for Customer使用移动设备访问系统的硬件要求
如果用平板电脑的话,推荐的设备列表: Android Samsung Galaxy Tab S2○ Processor: 2 x quad-core CPU -- 1.9 and 1.3 gigahe ...
- vue 实现走马灯效果
Part.1 问题 在写一个H5页面时遇到一个需求,头部公告需要滚动变换,需要实现一个走马灯效果 Part.2 实现 我的做法:利用 定时器 + CSS3 变换公告数组的顺序 从而实现走马灯效果 ...
- 菜鸟教你如何通俗理解——>集群、负载均衡、分布式
在“高并发,海量数据,分布式,NoSql,云计算......”概念满天飞的年代,相信不少朋友都听说过甚至常与人提起“集群,负载均衡”等,但不是所有人都有机会真正接触到这些技术,也不是所有人都真正理解了 ...
- java上传、下载、删除ftp文件
一共三个类,一个工具类Ftputil.,一个实体类Kmconfig.一个测试类Test 下载地址:http://download.csdn.net/detail/myfmyfmyfmyf/669710 ...
- ios之UISplitViewController
iPad的屏幕比iPhone大,所以在界面上,iPad比iPhone多一个UISplitViewController,用来实现iPad在横屏时,分两栏显示所需要的界面,可以一边是目录一边是具体的内容. ...
- 对Java提供的锁机制的一些思考
Java的数据会在CPU.Register.Cache.Heap和Thread stack之间进行复制操作,而前面四个都是在Java Threads之间共享,因此Java的锁机制主要用于解决Racin ...
- js 字符串加密
加密: 1.获得要加密的字符串:var str=input.value; 2.转化: for(var i=0;i<str.length;i++){ str+=String.fromCharCod ...