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-07-26 10:09 knowhow 閱讀(376) 評論(0)  編輯  收藏 所屬分類: Framework

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 郁南县| 泊头市| 棋牌| 吉水县| 平陆县| 黄冈市| 云浮市| 南部县| 尤溪县| 木兰县| 耿马| 永平县| 涟水县| 竹北市| 武平县| 张家口市| 桓仁| 九江县| 什邡市| 清流县| 府谷县| 西吉县| 霍山县| 东方市| 宣武区| 北宁市| 河南省| 宣恩县| 华坪县| 吉木萨尔县| 潼关县| 永德县| 弥渡县| 宣恩县| 东山县| 元江| 海原县| 年辖:市辖区| 杂多县| 德江县| 苏尼特左旗|