First Spring

          環(huán)境:MyEclipse6.0
          步驟:建立一個(gè)Java工程,選擇工程點(diǎn)擊右鍵->MyEclipse->Add Spring Capabilities
          寫一個(gè)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>
          寫一個(gè)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
          最后寫一個(gè)測(cè)試類:
          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());
              }
          }
          運(yùn)行SpringDemo,出現(xiàn)如下內(nèi)容:
          Hello!Epan Chen!


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


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


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

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(2)

          隨筆檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 东乡县| 兴隆县| 正宁县| 玛曲县| 高雄市| 龙口市| 汶川县| 安泽县| 卢氏县| 云林县| 巴林左旗| 三都| 哈巴河县| 武穴市| 潜山县| 隆尧县| 微山县| 通榆县| 光山县| 山阳县| 平定县| 分宜县| 新田县| 六枝特区| 渭源县| 新巴尔虎左旗| 永平县| 改则县| 钦州市| 和静县| 玉门市| 安顺市| 雷山县| 德格县| 美姑县| 响水县| 读书| 南充市| 茌平县| 昌黎县| 民丰县|