Topquan's Blog

          分享價值----成就你我----我的博客----你的家

          一個Spring程序

          Spring通過XML文件,完成bean配置和bean間依賴關系的注入。

          1.需要用到的包:
          spring-core.jar
          spring-beans.jar
          spring-context.jar
          commons-logging.jar

          2.Bean文件
          HelloBean.java
          package cn.blogjava.hello;

          import java.util.Date;

          public class HelloBean {
              
              
          private String helloWord;
              
          private String name;
              
          private Date date;
              
              
          public HelloBean() {
                  
              }

              
          public HelloBean(String helloWord, String name) {
                  
          this.helloWord = helloWord;
                  
          this.name = name;
              }    
              
              
          public String getHelloWord() {
                  
          return helloWord;
              }

              
          public void setHelloWord(String helloword) {
                  
          this.helloWord = helloword;
              }

              
          public String getName() {
                  
          return name;
              }

              
          public void setName(String name) {
                  
          this.name = name;
              }
              
              
          public Date getDate() {
                  
          return date;
              }

              
          public void setDate(Date date) {
                  
          this.date = date;
              }
          }

          配置文件
          beans-config.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="dateBean" class="java.util.Date"/>
              
          <bean id="helloBean" class="cn.blogjava.hello.HelloBean" >
                  
          <property name="helloWord">
                      
          <value>Hello!</value>
                  
          </property>
                  
          <property name="name">
                      
          <value>YYY!</value>
                  
          </property>    
                  
          <property name="date">
                      
          <ref bean="dateBean" />
                  
          </property>                
              
          </bean>
          </beans>

          3.測試程序
          SpringDemo.java
          package cn.blogjava.hello;

          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.FileSystemXmlApplicationContext;

          public class SpringDemo {
              
          public static void main(String[] args) {
                  ApplicationContext context 
          = 
                      
          new FileSystemXmlApplicationContext("beans-config.xml");        
                  HelloBean helloBean 
          = (HelloBean)context.getBean("helloBean");
                  System.out.print(
          "Name: ");
                  System.out.println(helloBean.getName());
                  System.out.print(
          "Word: ");
                  System.out.println(helloBean.getHelloWord());
                  System.out.println(helloBean.getDate());
              }
          }

          posted on 2006-08-05 00:56 topquan 閱讀(255) 評論(0)  編輯  收藏 所屬分類: Spring

          主站蜘蛛池模板: 满城县| 福贡县| 方城县| 启东市| 无极县| 眉山市| 泰宁县| 万源市| 西吉县| 龙岩市| 怀化市| 夹江县| 湟中县| 兴和县| 河东区| 闵行区| 嘉鱼县| 高唐县| 汝城县| 景德镇市| 孝感市| 祁门县| 乌海市| 丽水市| 呼图壁县| 托克逊县| 望谟县| 文水县| 南丹县| 庐江县| 龙胜| 梅州市| 沁水县| 北碚区| 汾阳市| 龙泉市| 保康县| 毕节市| 普兰县| 白沙| 家居|