First Spring

          環(huán)境:MyEclipse6.0
          步驟:建立一個Java工程,選擇工程點擊右鍵->MyEclipse->Add Spring Capabilities
          寫一個bean:
          package spring.main.bean;

          public class HelloBean {
              
          private String helloWord;

              
          public String getHelloWord() {
                  
          return helloWord;
              }

              
          public void setHelloWord(String helloWord) {
                  
          this.helloWord = helloWord;
              }
              
          }
          寫Spring配置文件application.xml:
          <?xml version="1.0" encoding="UTF-8"?>
          <beans
              
          xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation
          ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
              
              
          <bean id="helloBean"
                  class
          ="spring.main.bean.HelloBean">
                  
          <property name="helloWord">
                      
          <value>Hello!Epan Chen!</value>
                  
          </property>
              
          </bean>

          </beans>
          寫一個log4j.properties放在src目錄下:
          log4j.rootLogger=WARN, stdout
          log4j.appender.stdout
          =org.apache.log4j.ConsoleAppender
          log4j.appender.stdout.layout
          =org.apache.log4j.PatternLayout
          log4j.appender.stdout.layout.ConversionPattern
          =%%p [%c] - %m%n
          最后寫一個測試類:
          package spring.main.bean;

          import org.springframework.beans.factory.BeanFactory;
          import org.springframework.beans.factory.xml.XmlBeanFactory;
          import org.springframework.core.io.ClassPathResource;
          import org.springframework.core.io.Resource;

          public class SpringDemo {

              
          public static void main(String[] args) {
                  Resource rs 
          = new ClassPathResource("applicationContext.xml");
                  BeanFactory factory 
          = new XmlBeanFactory(rs);
                  HelloBean hello 
          = (HelloBean)factory.getBean("helloBean");
                  System.out.println(hello.getHelloWord());
              }
          }
          運行SpringDemo,出現(xiàn)如下內(nèi)容:
          Hello!Epan Chen!


          posted on 2008-10-07 14:12 育平 閱讀(242) 評論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          <2008年10月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(2)

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 绵阳市| 农安县| 滦南县| 天镇县| 鹤峰县| 大厂| 镇宁| 滦平县| 葵青区| 丹凤县| 平顶山市| 荆门市| 宣武区| 钟山县| 大兴区| 卫辉市| 华蓥市| 哈尔滨市| 扎囊县| 南漳县| 星座| 永泰县| 成都市| 宜川县| 故城县| 洛扎县| 宿松县| 靖远县| 清徐县| 新野县| 广汉市| 尤溪县| 苏州市| 祁门县| 五台县| 安龙县| 涟源市| 西乌珠穆沁旗| 六枝特区| 新乡市| 准格尔旗|