hello_en.properties文件内容
greeting=welcome my spring !
date=Today is {0} !
hello_zh.properties文件内容
greeting=欢迎光临我的第一个Spring !
date=今天是 {0} !
HelloAction.java
java 代码
- package com.gjx.spring;
- public class HelloAction {
- private String username;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String sayHello(String greeting){
- return username+", "+greeting;
- }
- }
beans.xml
xml 代码
- xml version="1.0" encoding="UTF-8"?> 测试结果:
- >
- <beans>
- <bean id="hello" class="com.gjx.spring.HelloAction" >
- <property name="username" value="冬国" />
- bean>
- <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
- <property name="basename">
- <value>hellovalue>
- property>
- bean>
- beans>
Junit的测试代码
TestSpring.java
java 代码
- package Test.com.gjx.spring;
- import java.io.BufferedOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Date;
- import java.util.Locale;
- import junit.framework.TestCase;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.context.support.GenericApplicationContext;
- import org.springframework.core.io.Resource;
- import com.gjx.spring.HelloAction;
- import com.gjx.spring.event.UserManager;
- public class TestSpring extends TestCase {
- public void testI18N() {
- ApplicationContext context = new ClassPathXmlApplicationContext(
- "beans.xml");
- String greeting = context.getMessage("greeting", null, Locale.CHINESE);
- String date = context.getMessage("date", new Object[] { new Date() },
- Locale.ENGLISH);
- HelloAction hello = (HelloAction) context.getBean("hello");
- System.out.println(hello.sayHello(greeting));
- System.out.println(date);
- }
- public void testResource() {
- ApplicationContext context = new GenericApplicationContext();
- Resource res1 = context.getResource("classpath:hello_zh.properties");
- Resource res2 = context
- .getResource("file:E:/MyJavaProject/Spring/src/hello_en.properties");
- if (res1.exists() && res2.exists()) {
- System.out.println(res1.getFilename());
- System.out.println(res2.getFilename());
- try {
- InputStream is1 = res1.getInputStream();
- InputStream is2 = res2.getInputStream();
- BufferedOutputStream bos = new BufferedOutputStream(System.out);
- byte[] bytes = new byte[100];
- int len;
- while ((len = is1.read(bytes)) != -1) {
- bos.write(bytes, 0, len);
- }
- while ((len = is2.read(bytes)) != -1) {
- bos.write(bytes, 0, len);
- }
- bos.close();
- is2.close();
- is1.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
冬国, 欢迎光临我的第一个Spring !
Today is 8/16/07 10:06 PM !
hello_zh.properties
hello_en.properties
greeting=\u6b22\u8fce\u5149\u4e34\u6211\u7684\u7b2c\u4e00\u4e2aSpring !
date=\u4eca\u5929\u662f\u3000 {0} !greeting=welcome my spring !
date=Today is {0} !