功能强大的 ApplicationContext
2012-01-20 12:48
148 查看
在Spring框架中,对Bean的管理有三种方式,即BeanWrapper、BeanFactory和ApplicationContext。三种方式中,又已ApplicationContext功能最强大,也最常用。
一、获取Bean
public class TestHelloWorldUseSpring {
public static void main(String[] args) {
ApplicationContext actx = new FileSystemXmlApplicationContext(
"/src/applicationContext.xml"); //打开和分析Spring配置文件
HelloWorld hw = (HelloWorld) actx.getBean("HelloWorld"); //反转控制,得到注入的类
System.out.println(hw.getMsg()); //使用注入的类
}
}
二、实现应用资源国际化
首先,在Spring的配置文件 applicationContext.xml 中,定义
org.springframework.context.support.ResourceBundleMessageSource 及其属性 basename,如下例:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>messages</value>
</property>
</bean>
<bean id="hw" class="beans.HelloWorld"></bean>
<bean id="date" class="java.util.Date"></bean>
</beans>
这里设置basename的值为 messages ,也就指定了存放信息资源的文件名必须是 messages.properties 或者 messages.class 。
然后,在src文件夹下面创建和编辑信息资源文件 messages.properties 。其内容如下:
WelcomeMsg=问候语: {0} 问候时间: {1}
利用 native2ascii 工具对 messages.properties 进行编码转换处理。
(Java 编译器和其它 Java 工具只能处理含有 Latin-1 和/或 Unicode 编码(udddd 记号)字符的文件。native2ascii 将含有其它字符编码的文件转换成含 Latin-1 和/或 Unicode 编码字符的文件)
最后,编写测试程序。
import java.util.Calendar;
import java.util.Locale;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class TestMessage {
public static void main(String[] args) {
ApplicationContext actx = new FileSystemXmlApplicationContext(
"/src/applicationContext.xml");
//生成消息参数
Object[] objs = new Object[] { "向全世界问好!",
Calendar.getInstance().getTime() };
//传递参数,生成输出结果
String msg=actx.getMessage("WelcomeMsg", objs,Locale.CHINA);
//输出结果
System.out.println(msg);
}
}
运行测试程序,控制台输出:
问候语: HelloWorld 问候时间: 07-7-23 下午4:05
进一步地,创建英文信息资源属性文件 messages_en_US.properties 文件,内容如下:
WelcomeMsg=English: {0} Time: {1}
测试程序中的 Locale.CHINA 改为
再次运行程序,结果如下:
. English: HelloWorld Time: 7/23/07 4:20 PM
可见,只要编辑了消息资源文件,原程序中值需要修改 Locale.XXXX 即可实现国际化支持。
三、灵活的资源访问
应用程序需要使用大量的资源,这些资源可以放在资源文件中,Spring提供三种访问资源文件的方式:
1、通过虚拟路径
ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");
Resource res=actx.getResource("classpath: messages.properties");
这里的 classpath:是Spring约定的虚拟路径。
2、通过实际路径访问
指定标准的URL,例如 file: http: 等。
ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");
Resource res=actx.getResource("file:d:/myjava/myproj/src/messages.properties");
3、通过相对路径访问
ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");
Resource res=actx.getResource("WEB-INF/src/messages.properties");
取得资源后,可以用 getFile() 访问资源文件的内容;可以用 exists() 检查资源文件是否存在; 通过 isOpen() 检查资源文件是否打开; 通过 getURL() 取得资源文件的URL。
四、事件传递
一、获取Bean
public class TestHelloWorldUseSpring {
public static void main(String[] args) {
ApplicationContext actx = new FileSystemXmlApplicationContext(
"/src/applicationContext.xml"); //打开和分析Spring配置文件
HelloWorld hw = (HelloWorld) actx.getBean("HelloWorld"); //反转控制,得到注入的类
System.out.println(hw.getMsg()); //使用注入的类
}
}
二、实现应用资源国际化
首先,在Spring的配置文件 applicationContext.xml 中,定义
org.springframework.context.support.ResourceBundleMessageSource 及其属性 basename,如下例:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>messages</value>
</property>
</bean>
<bean id="hw" class="beans.HelloWorld"></bean>
<bean id="date" class="java.util.Date"></bean>
</beans>
这里设置basename的值为 messages ,也就指定了存放信息资源的文件名必须是 messages.properties 或者 messages.class 。
然后,在src文件夹下面创建和编辑信息资源文件 messages.properties 。其内容如下:
WelcomeMsg=问候语: {0} 问候时间: {1}
利用 native2ascii 工具对 messages.properties 进行编码转换处理。
(Java 编译器和其它 Java 工具只能处理含有 Latin-1 和/或 Unicode 编码(udddd 记号)字符的文件。native2ascii 将含有其它字符编码的文件转换成含 Latin-1 和/或 Unicode 编码字符的文件)
最后,编写测试程序。
import java.util.Calendar;
import java.util.Locale;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class TestMessage {
public static void main(String[] args) {
ApplicationContext actx = new FileSystemXmlApplicationContext(
"/src/applicationContext.xml");
//生成消息参数
Object[] objs = new Object[] { "向全世界问好!",
Calendar.getInstance().getTime() };
//传递参数,生成输出结果
String msg=actx.getMessage("WelcomeMsg", objs,Locale.CHINA);
//输出结果
System.out.println(msg);
}
}
运行测试程序,控制台输出:
问候语: HelloWorld 问候时间: 07-7-23 下午4:05
进一步地,创建英文信息资源属性文件 messages_en_US.properties 文件,内容如下:
WelcomeMsg=English: {0} Time: {1}
测试程序中的 Locale.CHINA 改为
再次运行程序,结果如下:
. English: HelloWorld Time: 7/23/07 4:20 PM
可见,只要编辑了消息资源文件,原程序中值需要修改 Locale.XXXX 即可实现国际化支持。
三、灵活的资源访问
应用程序需要使用大量的资源,这些资源可以放在资源文件中,Spring提供三种访问资源文件的方式:
1、通过虚拟路径
ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");
Resource res=actx.getResource("classpath: messages.properties");
这里的 classpath:是Spring约定的虚拟路径。
2、通过实际路径访问
指定标准的URL,例如 file: http: 等。
ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");
Resource res=actx.getResource("file:d:/myjava/myproj/src/messages.properties");
3、通过相对路径访问
ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");
Resource res=actx.getResource("WEB-INF/src/messages.properties");
取得资源后,可以用 getFile() 访问资源文件的内容;可以用 exists() 检查资源文件是否存在; 通过 isOpen() 检查资源文件是否打开; 通过 getURL() 取得资源文件的URL。
四、事件传递
相关文章推荐
- iOS开发之窥探UICollectionViewController(四) --一款功能强大的自定义瀑布流
- DHTMLX JS框架,日程JS框架 功能强大 收藏!
- 功能强大的相片管理软件ACDSee Photo Manager 12.0.342注册码序列号中文版
- WIN7意想不到的强大功能
- 搜索表单制作语法:强大的搜索功能
- 防火墙iptables实现Linux强大的NAT功能
- 将vim 改造成功能强大的IDE系列之二
- 强大的web打印功能
- 【微软Visual Studio面世20周年巨制】全宇宙功能最强大IDE Visual Studio 2017 正式版发布
- Windows批处理功能配合脚本还是很强大的!(续)
- 功能强大的串口工具:GhostyComm 4.0(万能通讯精灵)
- SAP借HANA强大功能为生态系统注入新活力
- 【转】 推荐几款功能强大的PHP编辑器
- 新的 好用的 免费的 功能强大的Oracle 客户端
- 三款功能强大代码比较工具Beyond compare、DiffMerge、WinMerge
- iOS开发系列--Quartz 2D绘制2D图形和Core Image中强大的滤镜功能
- Linux强大的IO重定向和管道功能(内含命令替换,实例:批量删除文件)
- SuperSpider——打造功能强大的爬虫利器
- 功能强大的SendMessage函数