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
          主站蜘蛛池模板: 莲花县| 调兵山市| 自贡市| 年辖:市辖区| 万安县| 井陉县| 遵义市| 枣庄市| 雷州市| 武定县| 汽车| 彰化市| 闽清县| 福鼎市| 高青县| 揭西县| 贵定县| 武鸣县| 金坛市| 普兰店市| 融水| 昭觉县| 嘉祥县| 迁西县| 兴义市| 青阳县| 吴忠市| 济南市| 娱乐| 扶风县| 永泰县| 丹棱县| 新晃| 桐乡市| 白玉县| 绥阳县| 日喀则市| 大庆市| 宜丰县| 清新县| 贞丰县|