Topquan's Blog

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

          一個Spring程序

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

          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 閱讀(258) 評論(0)  編輯  收藏 所屬分類: Spring

          主站蜘蛛池模板: 汕头市| 彭山县| 原平市| 牟定县| 元谋县| 沙洋县| 黎平县| 通化市| 凤阳县| 南部县| 个旧市| 泽库县| 夏河县| 芷江| 汉阴县| 册亨县| 得荣县| 南康市| 高淳县| 阜新| 安仁县| 南汇区| 福贡县| 成武县| 观塘区| 吉木乃县| 桃园市| 福建省| 乌鲁木齐县| 德化县| 青神县| 石景山区| 嘉禾县| 札达县| 阿克苏市| 清远市| 伊吾县| 疏勒县| 集安市| 泊头市| 扎兰屯市|