自己做了两个spring例子依赖注入和国际化,里面还有一个侦听的简单实现,希望有能帮助到大家的地方;
下面是部分代码,附件里有完整的代码.
代码中符号 --<-- --> 换成 <>, 显示有不配对的属性自己增加吧.,我没法了.
下面是部分代码,附件里有完整的代码.
代码中符号 --<-- --> 换成 <>, 显示有不配对的属性自己增加吧.,我没法了.
applicationContext.xml中内容:
- --<--bean id="userJiHe" class="org.jiake.testSpring.User"
- abstract="false" lazy-init="default" autowire="byName"
- dependency-check="default"-->
- --<--property name="myhomeList"-->
- --<--list-->
- --<--ref bean="myhome" /-->
- --<--value-->YE,List is here!!--<--/value-->
- --<--ref bean="myhome" /-->
- --<--/list-->
- --<--/property-->
- --<--property name="myhomeSet"-->
- --<--set-->
- --<--ref bean="myhome" /-->
- --<--value-->YE,Set is here!!--<--/value-->
- --<--ref bean="myhome" /-->
- --<--/set-->
- --<--/property-->
- --<--property name="myhomeMap"-->
- --<--map-->
- --<--entry key="myhomeMap"-->
- --<--ref bean="myhome"/-->
- --<--/entry-->
- --<--entry key="myWord"-->
- --<--value-->Ye,Map is here!!--<--/value-->
- --<--/entry-->
- --<--/map-->
- --<--/property-->
- --<--/bean-->
- --<--bean id="messageSource"
- class="org.springframework.context.support.ResourceBundleMessageSource"
- abstract="false" lazy-init="default" autowire="default"
- dependency-check="default"-->
- --<--property name="basename"-->
- --<--value-->jiakeInter--<--/value-->
- --<--/property-->
- --<--/bean-->
- --<--bean id="say" class="org.jiake.testSpring.Say" abstract="false"
- lazy-init="default" autowire="default" dependency-check="default"-->
- --<--/bean-->
- --<--bean id="listener" class="org.jiake.testSpring.HaHaListener"
- abstract="false" lazy-init="default" autowire="default"
- dependency-check="default"-->
- --<--/bean-->
- Object objs[]=new Object[]{"jiakeInter01",(new Date().toString())}; String strContextInterCN=cntext.getMessage("jiake.welcome.word",objs,Locale.CHINA);
- String strContextInterUS=context.getMessage("jiake.welcome.word",objs,Locale.US);
下面是资源文件里的内容:
jiakeInter_en_US.properties
jiakeInter_en_US.properties
- 1. jiake.welcome.word=欢迎光临:{0} jiake example;现在时间:{1}
jiakeInter_zh_CN.properties
- 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"
这样的错误。
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.属性文件名的写法:
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.调用自定义的销毁方法
附件里的例子有不足请告知.^_^!!
3.配置messageSource这个bean(注意:一定是messageSource不是messageResource ,这是Spring规定的)例如:
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, "用户名不符合规则");也会出错.请问什么原因?
- 浏览: 22929 次
- 性别:

- 来自: 青岛

- 详细资料
搜索本博客
我的相册
111 046
共 4 张
共 4 张
最新评论
-
JAVA数据类型转换
谢谢楼主,说的很详细,好好学习下!
-- by sky21 -
JAVA数据类型转换
虽然是基础的东西, 但能总结出来还不错, 虽然有某些欠缺
-- by zhanjia -
15位和18位身份证的正则表 ...
不错,我收藏了
-- by jasongreen -
15位和18位身份证的正则表 ...
不错,我收藏了
-- by jasongreen -
DOS中 Orcle9i服务的启动 ...
呵呵
-- by jiake0504






评论排行榜