您的位置:首页 > 移动开发

功能强大的 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。

四、事件传递
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: