Spring 中资源的使用

hello_en.properties文件内容

greeting=welcome my spring !

date=Today is {0} !

hello_zh.properties文件内容

greeting=欢迎光临我的第一个Spring !

date=今天是  {0}  !

HelloAction.java

java 代码
  1. package com.gjx.spring;   
  2.   
  3. public class HelloAction {   
  4.     private String username;   
  5.   
  6.     public String getUsername() {   
  7.         return username;   
  8.     }   
  9.   
  10.     public void setUsername(String username) {   
  11.         this.username = username;   
  12.     }   
  13.        
  14.     public String sayHello(String greeting){   
  15.         return username+", "+greeting;   
  16.     }   
  17.   
  18. }   

 

beans.xml

xml 代码
  1. xml version="1.0" encoding="UTF-8"?>   测试结果:
  2. >  
  3.   
  4. <beans>  
  5.     <bean id="hello" class="com.gjx.spring.HelloAction" >  
  6.         <property name="username" value="冬国" />  
  7.     bean>  
  8.       
  9.     <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">  
  10.         <property name="basename">  
  11.           
  12.             <value>hellovalue>  
  13.         property>  
  14.     bean>  
  15. beans>  

Junit的测试代码

TestSpring.java

java 代码
  1. package Test.com.gjx.spring;   
  2.   
  3. import java.io.BufferedOutputStream;   
  4. import java.io.IOException;   
  5. import java.io.InputStream;   
  6. import java.util.Date;   
  7. import java.util.Locale;   
  8.   
  9. import junit.framework.TestCase;   
  10.   
  11. import org.springframework.context.ApplicationContext;   
  12. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  13. import org.springframework.context.support.GenericApplicationContext;   
  14. import org.springframework.core.io.Resource;   
  15.   
  16. import com.gjx.spring.HelloAction;   
  17. import com.gjx.spring.event.UserManager;   
  18.   
  19. public class TestSpring extends TestCase {   
  20.     public void testI18N() {   
  21.         ApplicationContext context = new ClassPathXmlApplicationContext(   
  22.                 "beans.xml");   
  23.         String greeting = context.getMessage("greeting"null, Locale.CHINESE);   
  24.         String date = context.getMessage("date"new Object[] { new Date() },   
  25.                 Locale.ENGLISH);   
  26.   
  27.         HelloAction hello = (HelloAction) context.getBean("hello");   
  28.         System.out.println(hello.sayHello(greeting));   
  29.         System.out.println(date);   
  30.     }   
  31.   
  32.     public void testResource() {   
  33.         ApplicationContext context = new GenericApplicationContext();   
  34.   
  35.         Resource res1 = context.getResource("classpath:hello_zh.properties");   
  36.         Resource res2 = context   
  37.                 .getResource("file:E:/MyJavaProject/Spring/src/hello_en.properties");   
  38.   
  39.         if (res1.exists() && res2.exists()) {   
  40.             System.out.println(res1.getFilename());   
  41.             System.out.println(res2.getFilename());   
  42.   
  43.             try {   
  44.                 InputStream is1 = res1.getInputStream();   
  45.                 InputStream is2 = res2.getInputStream();   
  46.                 BufferedOutputStream bos = new BufferedOutputStream(System.out);   
  47.   
  48.                 byte[] bytes = new byte[100];   
  49.                 int len;   
  50.                 while ((len = is1.read(bytes)) != -1) {   
  51.                     bos.write(bytes, 0, len);   
  52.                 }   
  53.                 while ((len = is2.read(bytes)) != -1) {   
  54.                     bos.write(bytes, 0, len);   
  55.                 }   
  56.                 bos.close();   
  57.                 is2.close();   
  58.                 is1.close();   
  59.   
  60.             } catch (IOException e) {   
  61.                 // TODO Auto-generated catch block   
  62.                 e.printStackTrace();   
  63.             }   
  64.         }   
  65.     }   
  66. }  

 

冬国, 欢迎光临我的第一个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} !

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值