posts - 32,comments - 8,trackbacks - 0

          本文講解一個不規范的spring quick start.

          1. 下載spring的插件包,什么版本之類的不用管了。反正能用。
          spring.jar http://www.boxcn.net/shared/yg306zac1h
          common-logging.jar http://www.boxcn.net/shared/ix93ziqljv

          2. 進入eclipse,File - New - Java Project.
          projectname = spring001 ===> Next
          在新建導向的第二頁,是Java Settings, 選擇Libraries -> Add External JARS -> 添加上面2個jar
          finish

          3. 進入Package Explorer, 在src下新建一個class.
          Package = com.java114.spring.test
          Name = HelloWordSpring
          再復選框:public static void main(String[] args) 鉤上

          4. 在HelloWordSpring.java 輸入以下的代碼
          package com.java114.spring.test;

          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 HelloWordSpring
          {
              
          private String msg;

              
          public void setMsg(String msg)
              {
                  
          this.msg = msg;
              }

              
          public void sayHello()
              {
                  System.out.println(msg);
              }

              
          public static void main(String[] args)
              {
                  Resource res 
          = new ClassPathResource("com/java114/spring/test/bean.xml");
                  BeanFactory factory 
          = new XmlBeanFactory(res);
                  HelloWordSpring hello 
          = (HelloWordSpring) factory.getBean("helloBean");
                  hello.sayHello();
              }

          }

          5. 在和HelloWordSpring.java 相同目錄下面,再新建一個xml文件,名字是bean.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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
           
          <bean id="helloBean" class="com.java114.spring.test.HelloWordSpring">
            
          <property name="msg" value="simple spring demo"/>
           
          </bean>
          </beans>
          為什么這樣寫,我也不知道,不管他。

          6. 鼠標右鍵選擇HelloWordSpring.java, 選擇Run As - Java Applications, 得到結果:
          2010-6-16 21:39:47 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
          信息: Loading XML bean definitions from class path resource [com/java114/spring/test/bean.xml]
          simple spring demo

          posted on 2010-06-16 20:13 張辰 閱讀(258) 評論(0)  編輯  收藏 所屬分類: Dr. Oops
          主站蜘蛛池模板: 襄樊市| 海林市| 辛集市| 丰都县| 大连市| 通城县| 万全县| 拉萨市| 吴江市| 新津县| 敦煌市| 凤山市| 固始县| 荥阳市| 凤阳县| 邳州市| 迁西县| 林甸县| 武强县| 新宾| 绿春县| 贵阳市| 金门县| 霍林郭勒市| 土默特右旗| 伽师县| 安仁县| 巴林左旗| 台江县| 辉县市| 莎车县| 霍州市| 剑阁县| 延川县| 嘉兴市| 七台河市| 汕尾市| 华蓥市| 澄城县| 江城| 嘉义县|