First Spring

          環境: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,出現如下內容:
          Hello!Epan Chen!


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


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


          網站導航:
           
          <2008年10月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 佳木斯市| 平顺县| 堆龙德庆县| 育儿| 辽宁省| 玉田县| 镇巴县| 柳河县| 巨野县| 阿鲁科尔沁旗| 奎屯市| 琼海市| 汉源县| 思茅市| 安顺市| 工布江达县| 乌兰察布市| 阳信县| 平原县| 苏州市| 苏尼特左旗| 黄山市| 龙游县| 铁岭市| 周宁县| 天镇县| 华宁县| 余干县| 房产| 桑日县| 青浦区| 屏东市| 桂阳县| 高平市| 荥阳市| 河池市| 娱乐| 南和县| 东台市| 新安县| 隆子县|