Spring入门经典例子

来源:互联网 发布:ios程序员简历 编辑:程序博客网 时间:2024/04/30 14:27

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 test.lyx;  
  2. import java.util.Date;  
  3. import java.util.Locale;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  6. publicclass TestMain {  
  7.     publicstaticvoid main(String[] args) {  
  8.         ApplicationContext context=new FileSystemXmlApplicationContext("test/lyx/applicationContext.xml");  
  9.             Object[] obj=newObject[]{"lyx",new Date()};  
  10.             System.out.println(context.getMessage("welcome.label",obj, Locale.CHINA));  
  11.             System.out.println(context.getMessage("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文件.必须通过,native2asciiUTF-8才好用.转换后如下:
welcome.label=/u6b22/u8fce/u4f60 {0} :/u65f6/u95f4 {1} 
上面我是用了国际的插件,会自动转换.
   

   

spring bean的生命周期

分为定义,初始化,使用,消亡
写个例子测试一下:
第一步:建一个类User,代码如下:
java 代码
  1. package test.lyx;   
  2. publicclass User {   
  3.     private String userName;   
  4.     public String getUserName() {   
  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 test.lyx;   
  2. import org.springframework.context.ApplicationContext;   
  3. import org.springframework.context.support.FileSystemXmlApplicationContext;   
  4. publicclass TestMain {   
  5.     publicstaticvoid main(String[] args) {   
  6.     ApplicationContext context=new FileSystemXmlApplicationContext("test/lyx/applicationContext.xml");   
  7.         User user1=(User)context.getBean("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-methoddestroy-method两个方法.
我们把User类修改一下:代码如下:
java 代码
  1. package test.lyx;   
  2. import org.springframework.beans.factory.InitializingBean;   
  3. import org.springframework.beans.factory.DisposableBean;   
  4. publicclass User implements InitializingBean,DisposableBean{   
  5.     private String userName;   
  6.     public String getUserName() {   
  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.     publicvoid afterPropertiesSet() throws Exception {   
  19.         this.userName="wangwu";// TODO Auto-generated method stub   
  20.     }   
  21.     
  22.     publicvoid destroy() throws Exception {   
  23.         this.userName=null;// TODO Auto-generated method stub   
  24.         }   
  25. }   
修改过后的类,就是在原来的基础上,实现两个接口InitializingBean,DisposableBean也就是初始化和消毁的接口,这里我们要把InitializingBean接口里的afterPropertiesSet方法给覆盖掉,也就是将初始化的东西写在这个方法以里面.同时也要把DisposableBean接口的destroy方法覆盖掉,
消毁的东西写在这个方法里.这样的话,就无需在配置文件中配置init-methoddestroy-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 test.lyx;   
  2. publicclass User {   
  3.     private String userName;   
  4.     public String getUserName() {   
  5.         return userName;   
  6.     }   
  7.     publicvoid setUserName(String userName) {   
  8.         this.userName = userName;   
  9.     }   
  10. }   
第五步:创建一个类TestUser.代码如下:(用于测试用)
java 代码
  1. package test.lyx;   
  2. import org.springframework.context.ApplicationContext;   
  3. import org.springframework.context.support.FileSystemXmlApplicationContext;   
  4. public class TestUser {   
  5.     public static void main(String[] args) {   
  6.          ApplicationContext context=new FileSystemXmlApplicationContext("/src/test/lyx/applicationContext.xml");   
  7.         User user=(User)context.getBean("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.