Spring国际化实例

来源:互联网 发布:cf手游机枪数据 编辑:程序博客网 时间:2024/06/13 00:25

Spring国际化实例 - 398198920 - 冰冻三尺非一日之寒

MainDemo.java

package org.eimhe;

import java.util.Date;
import java.util.Locale;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class MainDemo {

 public static void main(String[] args) {
  ApplicationContext context=null;
  context=new FileSystemXmlApplicationContext("org/eimhe/applicationContext.xml");
  UserBean user=(UserBean)context.getBean("user");
  System.out.println("username="+user.getUsername());
  System.out.println("age="+user.getAge());
  Object[] objs=new Object[]{"syb",new Date().toString()};

//www.eimhe.com为资源文件的key值,objs为资源文件value值所需要的参数,Local.CHINA为国际化为什么语言
        String str=context.getMessage("
www.eimhe.com", objs, Locale.CHINA);
        System.out.println(str);
 }

}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>


 <bean id="user" class="org.eimhe.UserBean" abstract="false"
  singleton="true" lazy-init="default" autowire="default"
  dependency-check="default" destroy-method="clear">
 </bean>
 
 <bean id="messageSource"
  class="org.springframework.context.support.ResourceBundleMessageSource"
  abstract="false" singleton="true" lazy-init="default"
  autowire="default" dependency-check="default">
  <property name="basenames">
  <list>
   <value>eimhe</value>
  </list>
  </property>
 </bean></beans>

 

eimhe_en_US.properties

www.eimhe.com=welcome {0},time:{1}

eimhe_zh_CN.properties

www.eimhe.com=欢迎 {0},时间:{1}

UserBean.java

package org.eimhe;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.DisposableBean;
public class UserBean implements InitializingBean,DisposableBean{
 private String username;
 private int age;
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 public void init(){
  this.username="zhangsan";
  this.age=20;
 }
 public void clear(){
  this.username="";
  this.age=0;
 }
 public void afterPropertiesSet() throws Exception {
  this.username="wangwu";
  this.age=20; 
 }
 public void destroy() throws Exception {
  this.username="";
  this.age=0;  
 }
}

0 0