自己做了两个spring例子依赖注入和国际化,里面还有一个侦听的简单实现,希望有能帮助到大家的地方;
下面是部分代码,附件里有完整的代码.  
代码中符号  
--<-- -->  换成  <>,  显示有不配对的属性自己增加吧.,我没法了.

applicationContext.xml中内容:
 
  1. --<--bean  id="userJiHe" class="org.jiake.testSpring.User"  
  2.         abstract="false" lazy-init="default" autowire="byName"  
  3.         dependency-check="default"-->  
  4.         --<--property name="myhomeList"-->  
  5.             --<--list-->  
  6.                 --<--ref bean="myhome" /-->  
  7.                 --<--value-->YE,List is here!!--<--/value-->  
  8.                 --<--ref bean="myhome" /-->  
  9.             --<--/list-->             
  10.         --<--/property-->  
  11.         --<--property name="myhomeSet"-->  
  12.             --<--set-->  
  13.                 --<--ref bean="myhome" /-->  
  14.                 --<--value-->YE,Set is here!!--<--/value-->  
  15.                 --<--ref bean="myhome" /-->  
  16.             --<--/set-->  
  17.         --<--/property-->  
  18.         --<--property name="myhomeMap"-->  
  19.             --<--map-->  
  20.                 --<--entry key="myhomeMap"-->  
  21.                     --<--ref bean="myhome"/-->                
  22.                 --<--/entry-->  
  23.                 --<--entry key="myWord"-->  
  24.                     --<--value-->Ye,Map is here!!--<--/value-->  
  25.                 --<--/entry-->  
  26.             --<--/map-->  
  27.         --<--/property-->  
  28.     --<--/bean-->  
  29.     --<--bean id="messageSource"  
  30.         class="org.springframework.context.support.ResourceBundleMessageSource"  
  31.         abstract="false" lazy-init="default" autowire="default"  
  32.         dependency-check="default"-->  
  33.         --<--property name="basename"-->  
  34.             --<--value-->jiakeInter--<--/value-->  
  35.         --<--/property-->  
  36.     --<--/bean-->  
  37.       
  38.     --<--bean id="say" class="org.jiake.testSpring.Say" abstract="false"  
  39.         lazy-init="default" autowire="default" dependency-check="default"-->  
  40.     --<--/bean-->  
  41.     --<--bean id="listener" class="org.jiake.testSpring.HaHaListener"  
  42.         abstract="false" lazy-init="default" autowire="default"  
  43.         dependency-check="default"-->  
  44.     --<--/bean-->  

  1.   Object objs[]=new Object[]{"jiakeInter01",(new Date().toString())};   String strContextInterCN=cntext.getMessage("jiake.welcome.word",objs,Locale.CHINA);  
  2.   String strContextInterUS=context.getMessage("jiake.welcome.word",objs,Locale.US);  
下面是资源文件里的内容:

jiakeInter_en_US.properties
  1. 1. jiake.welcome.word=欢迎光临:{0}  jiake example;现在时间:{1}    
jiakeInter_zh_CN.properties
  1. jiake.welcome.word=welcome:{0} jiake example;time:{1}  
用Spring做国际化时经常会报:
org.springframework.context.NoSuchMessageException: No message found under code 'userlogin' for locale 'zh_CN'.
at org.springframework.context.support.DelegatingMessageSource.getMessage(DelegatingMessageSource.java:65)
at org.springframework.context.support.AbstractApplicationContext.getMessage(AbstractApplicationContext.java:646)
at com.neusoft.Test.Test.main(Test.java:43)
Exception in thread "main"
这样的错误。
总结一下,注意以下几点:
1.新建资源文件时,尽量右击项目新建文件,来增加.
2.属性文件名的写法:
           messages_zh_CN.properties  (中文)
           messages_en_US.properties  (英文)
3.
配置messageSource这个bean(注意:一定是messageSource不是messageResource ,这是Spring规定的)例如:
    --<--bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default"
-->
        --<--property name="basename"-->
            --<--value-->messages</--value-->
        --<--/property-->
    --<--/bean-->

注意:红色的标记名字要对应. 以上几点考虑到,应该就差不多了.

Bean的生命周期:

1.Bean的构造

2.调用setXXX()方法设置Bean的属性

3.调用BeanNmaeAware的setBeanName()

4.调用BeanFactoryAware的setBeanFactory方法

5.调用BeanPostProcessor的postProcesBeforInitialization方法

6.调用InitializingBean的afterPropertiesSet()方法

7.调用自定义的初始化方法

8.调用BeanPostProcessor类的postProcessAfterInitialization()方法

9.调用DisposableBean的destroy()方法

10.调用自定义的销毁方法
附件里的例子有不足请告知.^_^!!

评论
ningshuihan 2007-09-17
我现在跟你的做法一样,但是在页面用spring:message 输出消息的时候还是出错,No message found under code 'XXX' for locale 'zh_CN',在validator里这样errors.rejectValue("username", "users.invalid_username", null, "用户名不符合规则");也会出错.请问什么原因?