Topquan's Blog

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

          導航

          <2025年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          隨筆檔案(2)

          文章分類(36)

          相冊

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

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

          主站蜘蛛池模板: 正安县| 濮阳市| 宁波市| 桓仁| 隆林| 平昌县| 柘城县| 宜兰市| 嘉定区| 米林县| 高邮市| 文山县| 阿城市| 西平县| 中超| 湘阴县| 修文县| 泾阳县| 兴城市| 盘锦市| 尉氏县| 西青区| 麦盖提县| 霸州市| 饶阳县| 和静县| 思茅市| 湘潭县| 上犹县| 金阳县| 公安县| 桦川县| 凉山| 大同市| 比如县| 东丰县| 阿克苏市| 天水市| 苏州市| 黔西| 扶余县|