新的起點(diǎn)

          新的起點(diǎn)
          隨筆 - 9, 文章 - 1, 評(píng)論 - 1, 引用 - 0

          導(dǎo)航

          <2008年1月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章檔案

          相冊(cè)

          個(gè)人常用網(wǎng)站

          其它

          開(kāi)發(fā)類網(wǎng)站

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          Spring自學(xué)筆記(1)

           

          用了一段時(shí)間的公司框架,還沒(méi)時(shí)間認(rèn)真的坐下來(lái)練習(xí)一下Spring開(kāi)源框架的基礎(chǔ),真是有些不好意思。剛看完在網(wǎng)上下的關(guān)于Spring的視頻教程,正好練習(xí)一下,就以水果作為今天例子程序的主角。

          打開(kāi)Myeclipse,添加對(duì)Spring的支持(也不知道這么選行不行,汗~)

                 接下來(lái)按照默認(rèn)設(shè)置直接點(diǎn)擊Next,最后點(diǎn)完成即可。項(xiàng)目中生成了傳說(shuō)中功能強(qiáng)大的Spring配置文件applicationContext.xml,哈哈,親一個(gè)!

                 環(huán)境設(shè)置好了,接下來(lái)該編寫(xiě)類文件了。先生成一個(gè)Fruit接口:

          1package com.haiing.myspring;
          2
          3public interface Fruit 
          4
          5{
          6       //吃水果
          7       public void eat();
          8}

          9

                 接著編寫(xiě)實(shí)現(xiàn)此接口的類文件:

           1package com.haiing.myspring;
           2
           3public class Apple implements Fruit {
           4
           5  public void eat() {
           6
           7              // 吃蘋(píng)果
           8
           9              System.out.println("我要吃蘋(píng)果了~~~") ;       
          10
          11       }

          12
          13}

          14


           1package com.haiing.myspring;
           2
           3public class potato implements Fruit 
           4{
           5       public void eat() 
           6       {
           7
           8            // 吃西紅柿
           9
          10              System.out.println("我要吃西紅柿了~~~") ;
          11       }

          12}

          13


           1package com.haiing.myspring;
           2
           3public class Orange implements Fruit {
           4
           5       public void eat() {
           6
           7              // 吃橘子
           8
           9              System.out.println("我要吃橘子了~~~") ;
          10
          11       }

          12
          13}

          14


                 接口文件寫(xiě)好了,現(xiàn)在要寫(xiě)測(cè)試類和配置Spring文件了。

                 設(shè)置applicationContext.xml配置文件如下:

          <?xml version="1.0" encoding="UTF-8"?>

          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

          <beans>

              
          <bean id="apple" class="com.haiing.myspring.Apple"></bean>

              
          <bean id="potato" class="com.haiing.myspring.Potato"></bean>

              
          <bean id="orange" class="com.haiing.myspring.Orange"></bean>

          </beans>


                 編寫(xiě)一個(gè)測(cè)試類文件:

           1package com.haiing.myspring;
           2
           3import org.springframework.context.ApplicationContext;
           4
           5import org.springframework.context.support.ClassPathXmlApplicationContext;
           6
           7public class TestFruit 
           8
           9{
          10       public static void main(String[] args)
          11       {
          12
          13              ApplicationContext context = null ;
          14
          15              context = new ClassPathXmlApplicationContext("applicationContext.xml") ;
          16
          17              Fruit fruit = (Fruit)context.getBean("orange") ; 
          18
          19              fruit.eat() ;            
          20       }

          21}

          22

           

          現(xiàn)在可以開(kāi)始測(cè)試效果了,運(yùn)行測(cè)試類,在控制臺(tái)輸出:

          我要吃蘋(píng)果了~~~

          log4j:WARN No appenders could be found for logger (org.springframework.beans.factory.xml.XmlBeanDefinitionReader).

          log4j:WARN Please initialize the log4j system properly.

          大功告成,下面紅字提示是因?yàn)闆](méi)有設(shè)置日志。這樣,在蘋(píng)果漲價(jià)之后,我們就可以方便的修改測(cè)試文件,換成其他的水果,照吃不誤了~

          不過(guò)話又說(shuō)回來(lái)了,換歸換,吃其他的東西可是吃不出蘋(píng)果的味道呀

          posted on 2008-01-29 08:19 軋鋼王子 閱讀(290) 評(píng)論(0)  編輯  收藏 所屬分類: Spring


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 宁蒗| 郧西县| 昌邑市| 台北市| 乌拉特中旗| 阿合奇县| 炉霍县| 湘乡市| 巴楚县| 尉犁县| 鄂温| 遂昌县| 苍梧县| 井研县| 凭祥市| 马鞍山市| 莲花县| 德清县| 内丘县| 手游| 水富县| 江西省| 马山县| 浮山县| 安顺市| 青冈县| 汉阴县| 灌南县| 察哈| 文成县| 西藏| 彰武县| 梧州市| 贞丰县| 额济纳旗| 建湖县| 罗田县| 长沙县| 富顺县| 咸宁市| 汉寿县|