新的起點

          新的起點
          隨筆 - 9, 文章 - 1, 評論 - 1, 引用 - 0
          數(shù)據(jù)加載中……

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

           

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

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

                 接下來按照默認設(shè)置直接點擊Next,最后點完成即可。項目中生成了傳說中功能強大的Spring配置文件applicationContext.xml,哈哈,親一個!

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

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

          9

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

           1package com.haiing.myspring;
           2
           3public class Apple implements Fruit {
           4
           5  public void eat() {
           6
           7              // 吃蘋果
           8
           9              System.out.println("我要吃蘋果了~~~") ;       
          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àn)在要寫測試類和配置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>


                 編寫一個測試類文件:

           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)在可以開始測試效果了,運行測試類,在控制臺輸出:

          我要吃蘋果了~~~

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

          log4j:WARN Please initialize the log4j system properly.

          大功告成,下面紅字提示是因為沒有設(shè)置日志。這樣,在蘋果漲價之后,我們就可以方便的修改測試文件,換成其他的水果,照吃不誤了~

          不過話又說回來了,換歸換,吃其他的東西可是吃不出蘋果的味道呀

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


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 天气| 松阳县| 增城市| 济阳县| 昌宁县| 成安县| 浦江县| 西吉县| 呈贡县| 多伦县| 平湖市| 洱源县| 玉树县| 鹤岗市| 钟祥市| 金华市| 东辽县| 平谷区| 肇源县| 邹平县| 休宁县| 黄山市| 基隆市| 马鞍山市| 洛川县| 饶平县| 巨野县| 开化县| 汽车| 尚志市| 康马县| 武陟县| 大同市| 宜黄县| 甘泉县| 札达县| 西贡区| 怀远县| 惠州市| 鄯善县| 广平县|