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.測(cè)試程序
          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) 評(píng)論(0)  編輯  收藏 所屬分類: Framework

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 双桥区| 正宁县| 晋江市| 神农架林区| 石阡县| 石首市| 黔西| 长沙市| 福州市| 神农架林区| 和田县| 米泉市| 龙山县| 腾冲县| 含山县| 延吉市| 青海省| 九龙县| 彰武县| 紫云| 凤台县| 尉犁县| 乌拉特中旗| 军事| 临沂市| 资阳市| 阿瓦提县| 乌拉特前旗| 昌平区| 潍坊市| 田林县| 吴堡县| 禄劝| 裕民县| 隆德县| 平山县| 岐山县| 拉萨市| 林州市| 山东省| 临海市|