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)

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 耿马| 张家界市| 卓资县| 浦县| 闵行区| 同心县| 汨罗市| 年辖:市辖区| 泌阳县| 阿勒泰市| 宁武县| 额尔古纳市| 遂平县| 聊城市| 蕲春县| 防城港市| 宁南县| 齐齐哈尔市| 登封市| 呼和浩特市| 沽源县| 汕头市| 宝应县| 木里| 庐江县| 运城市| 嘉义县| 罗源县| 甘孜县| 苏尼特左旗| 手机| 宁德市| 和平县| 子洲县| 邯郸县| 高淳县| 临安市| 靖江市| 东乌珠穆沁旗| 梅州市| 常宁市|