spring的国际化,超简单的例子

可以通过如下三种方式:



beanWapper



beanFactory



applicationContext



说明:



beanWapper已经不推荐使用了



beanFactory提供一种先进的管理机制来管理bean



applicationContext新增了许多的新的功能,如果国际化,获取资源,事件的传递



这里我以国际为例:


建两个properties文件用于国际化.       


        Yuki_en_US.proerties代码如下:       


        welcome.label        =        welcome {0} :time {1}       


        Yuki_zh_CN.proerties代码如下:       


        welcome.label        =        欢迎你         {0} :        时间         {1}
         applicationContext.xml         配置如下:



xml 代码




 


1. <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"           abstract="false" singleton="true" lazy-init="default"
2. autowire="default" dependency-check="default">
3. <property name="basename">
4. <value>yuki</value>
5. </property>
6. ean>




这里需要注意两点:第一, id 的名字必须是 messageSource. 属性 basename 的值为 properties 文件的前缀.

java 代码



1. package
2. import
3. import
4. import
5. import
6. publicclass TestMain {   
7.     publicstaticvoid main(String[] args) {   
8. new FileSystemXmlApplicationContext("test/lyx/applicationContext.xml");   
9. "lyx",new
10. "welcome.label",obj, Locale.CHINA));   
11. "welcome.label",obj, Locale.US));   
12.     }   
13. }


运行结果会在控制台上打出:



欢迎你 lyx : 时间 07-1-18 上午 10:38



welcome lyx :time 1/18/07 10:38 AM



注意: Yuki_zh_CN.proerties 文件.必须通过, native2ascii 成 UTF-8 才好用.转换后如下:


welcome.label=/u6b22/u8fce/u4f60 {0} :/u65f6/u95f4 {1}


spring bean的生命周期

分为定义,初始化,使用,消亡



写个例子测试一下:




java 代码



1. package
2. publicclass User {    
3. private
4. public
5.         returnuserName;    
6.     }    
7.     publicvoid setUserName(String userName) {    
8. this.userName = userName;    
9.     }    
10. }



第二步:将 User 注入. applicationContext.xml 配置如下:

xml 代码



1. <?xml version="1.0" encoding="UTF-8"?>
2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
3. <beans>
4. <bean id="user" class="test.lyx.User" abstract="false"
5. singleton="true" lazy-init="default" autowire="byType"
6. dependency-check="default">
7. <property name="userName">
8. <value>liuyuanxi</value>
9. </property>
10. </bean>
11. </beans>


第三步:建一个类TestMain测试一下:


java 代码




1. package
2. import
3. import
4. publicclass TestMain {    
5.     publicstaticvoid main(String[] args) {    
6. new FileSystemXmlApplicationContext("test/lyx/applicationContext.xml");    
7. "user");    
8.         System.out.println(user1.getUserName());    
9.     }    
10. }




这样运行该类会打出 liuyuanxi .那么这几步就是 bean 的定义和使用.



接下来我们在 User 类中加上两个方法: init clear, 当然方法明任你起.




java 代码



1. publicvoid init(){    
2. this.userName="zhangsan";    
3.    }    
4.    publicvoid clear(){    
5. this.userName=null;    
6.    }

然后将配置文件修改如下:


xml 代码



1. <?xml version="1.0" encoding="UTF-8"?>
2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
3. <beans>
4. <bean id="user" class="test.lyx.User" abstract="false"
5. singleton="true" lazy-init="default" autowire="byType"
6. dependency-check="default" init-method="init" destroy-method="clear">
7. <property name="userName">
8. <value>liuyuanxi</value>
9. </property>
10. </bean>
11. </beans>


这里面的红线部分就是修改的内容.如果 init-method 属性设置了方法,那么就会在 bean 初始化的时候被执行.而 destroy-method 在消毁之前执行.



第二种方法:(实现初始化,和消毁的两个接口)



可以不在配置文件中指定 init-method , destroy-method 两个方法.



类修改一下:代码如下:

java 代码




1. package
2. import
3. import
4. publicclass User implements
5. private
6. public
7.         returnuserName;    
8.     }    
9.     publicvoid setUserName(String userName) {    
10. this.userName = userName;    
11.     }    
12.     publicvoid init(){    
13. this.userName="zhangsan";    
14.     }    
15.     publicvoid clear(){    
16. this.userName=null;    
17.     }    
18. throws
19. this.userName="wangwu";// TODO Auto-generated method stub 
20.     }    
21.      
22. throws
23. this.userName=null;// TODO Auto-generated method stub 
24.         }    
25. }




修改过后的类,就是在原来的基础上,实现两个接口 InitializingBean,DisposableBean 也就是初始化和消毁的接口,这里我们要把 InitializingBean 接口里的 afterPropertiesSet 方法给覆盖掉,也就是将初始化的东西写在这个方法以里面.同时也要把 DisposableBean 接口的 destroy 方法覆盖掉,



消毁的东西写在这个方法里.这样的话,就无需在配置文件中配置 init-method 和 destroy-method 两个方法.



整个的过程就是 bean 的生命周期.


Spring的hello world程序

Spring目前较为流行的框架之一.核心技术.DI,AOP



虽然不是一个完整的java规范,但在j2ee的开发领域却占着重要的比例.



目前较为流行的SSH体系结构.Struts用于表示层,Spring用于控制层,而hibernate用于数据库的持久层.而Spring在其中却成了其中最较重要的部分.



Spring编写hello world



编写环境eclipse3.2.1,myeclipse 5.1



第一步:创建一个web项目spring1.



第二步:创建一个包,把涉及到的几个类和配置文件放到包中.这里我的包名为test.lyx



第三步:加入spring capabilities..这里我们加入核心包就可以了.还没有用到其它的技术.这时需要产生一个配置文件spring必不可少的.这里我以applicationContext.xml命名.



第四步:创建一个类User.代码如下:




java 代码



1. package
2. publicclass User {    
3. private
4. public
5.         return userName;    
6.     }    
7.     publicvoid setUserName(String userName) {    
8. this.userName = userName;    
9.     }    
10. }





java 代码




    1. package
    2. import
    3. import
    4. public class
    5. public static void
    6. new FileSystemXmlApplicationContext("/src/test/lyx/applicationContext.xml");    
    7. "user");    
    8.         System.out.print(user.getUserName());    
    9.     }    
    10. }


    第六步:修改applicationContext.xml文件代码如下:


    xml 代码


    1. <!--sp-->xml version="1.0" encoding="UTF-8"?>
    2. <!--CTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"</sp-->>
    3.      
    4. <beans>
    5. <bean id="user" class="test.lyx.User" abstract="false"
    6. singleton="true" lazy-init="default" autowire="default"
    7. dependency-check="default">
    8. <property name="userName">
    9. <value>hello liuyuanxivalue>
    10. property>
    11. bean>
    12. beans>




    这一步就是注入的过程.所谓注入就是由容器控制程序之间的关系,在运行的时候给予所有指定的值.



    进行 TestUser 你就会看 .hello liuyuanxi.