posts - 32,comments - 8,trackbacks - 0

          本文講解一個(gè)不規(guī)范的spring quick start.

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

          2. 進(jìn)入eclipse,F(xiàn)ile - New - Java Project.
          projectname = spring001 ===> Next
          在新建導(dǎo)向的第二頁,是Java Settings, 選擇Libraries -> Add External JARS -> 添加上面2個(gè)jar
          finish

          3. 進(jìn)入Package Explorer, 在src下新建一個(gè)class.
          Package = com.java114.spring.test
          Name = HelloWordSpring
          再復(fù)選框: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 相同目錄下面,再新建一個(gè)xml文件,名字是bean.xml, 內(nèi)容如下
          <?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. 鼠標(biāo)右鍵選擇HelloWordSpring.java, 選擇Run As - Java Applications, 得到結(jié)果:
          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 張辰 閱讀(251) 評(píng)論(0)  編輯  收藏 所屬分類: Dr. Oops
          主站蜘蛛池模板: 双鸭山市| 安多县| 杭锦旗| 鄂托克前旗| 嘉兴市| 临沧市| 阜康市| 同心县| 晋中市| 射阳县| 固镇县| 修水县| 二手房| 大厂| 米脂县| 奉节县| 枣阳市| 达拉特旗| 车险| 当阳市| 泊头市| 洱源县| 黑龙江省| 疏附县| 宝山区| 布拖县| 崇明县| 鲁山县| 得荣县| 民乐县| 开封市| 邵阳市| 德昌县| 保山市| 乌鲁木齐市| 中阳县| 通化县| 海林市| 龙胜| 玉田县| 乌鲁木齐市|