Junky's IT Notebook

          統(tǒng)計(jì)

          留言簿(8)

          積分與排名

          WebSphere Studio

          閱讀排行榜

          評(píng)論排行榜

          Spring in Action 筆記(I)

          在考試前匆匆看了一遍Spring in Action 也沒(méi)有記錄什么,考試結(jié)束了,要抓緊時(shí)間整理一下.要不然就忘了.^_^:

          整理一下我目前可能會(huì)用到的模塊, 對(duì)于那些現(xiàn)在根本用不到的冬冬還是等有時(shí)間再研究吧!

          第一個(gè)當(dāng)然是最經(jīng)典的HelloWorld 了 , 呵呵,簡(jiǎn)單,但是說(shuō)明了原理.

          定義一個(gè)服務(wù)接口

          package? test.helloworld;

          public?interface? GreetingService?{
          ?? public?void? sayGreeting();

          }

          下面是其實(shí)現(xiàn):

          package? test.helloworld;

          public?class? GreetingServiceImpl? implements? GreetingService?{

          ?? private? String?greeting;
          ?? public? GreetingServiceImpl()?{}
          ?? public? GreetingServiceImpl(String?greeting)?{
          ???? this .greeting?=?greeting;
          ?? }
          ?? public?void? sayGreeting()?{
          ???? //??Auto-generated?method?stub
          ???? System.out.println(greeting);
          ?? }
          ??
          ?? public?void? setGreeting(String?greeting)?{
          ???? this .greeting?=?greeting;
          ?? }

          }

          然后就是測(cè)試 IoC 的測(cè)試代碼:

          package? test.helloworld;


          import? org.springframework.beans.factory.BeanFactory;
          import? org.springframework.beans.factory.xml.XmlBeanFactory;
          import? org.springframework.context.ApplicationContext;
          import? org.springframework.context.support.ClassPathXmlApplicationContext;
          import? org.springframework.core.io.FileSystemResource;

          public?class? HelloApp?{

          ?? /**
          ??? *? @param? args
          ??? */
          ?? public?static?void? main(String[]?args)?{
          ???? //?TODO?Auto-generated?method?stub
          //????BeanFactory?factory;
          //????factory?=?new?XmlBeanFactory(new?FileSystemResource("src/test/helloworld/hello.xml"));
          ???? //?使用不同的方法得到bean.?(BeanFactory?or?ApplicationContext)
          ???? ApplicationContext?context?=? new? ClassPathXmlApplicationContext( "test/helloworld/hello.xml" );
          ????
          ????
          ???? GreetingService?greetingService?=?(GreetingService)?context.getBean( "greetingService" );
          //????GreetingService?greetingService?=?(GreetingService)?factory.getBean("greetingService");
          ???? greetingService.sayGreeting();
          ?? }

          }

          還有重要的配置文件如下:hello.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 = "greetingService"

          class = "test.helloworld.GreetingServiceImpl" >

          < property name = "greeting" >

          < value > ice rain ! </ value >

          </ property >

          </ bean >

          </

          beans >

          呵呵就這么簡(jiǎn)單,實(shí)現(xiàn)了 greeting 屬性的 Ioc.

          這是Spring 兩大基本支柱其一的工作原理, 關(guān)于A(yíng)oP的內(nèi)容,在這里不作討論,因?yàn)楝F(xiàn)在我使用AoP的地方不是很多,簡(jiǎn)單的應(yīng)用是很簡(jiǎn)單的啦.^_^.

          下面一篇我們來(lái)看看 在spring包裝下的jdbc訪(fǎng)問(wèn). 詳細(xì)情況也可以參考這里

          posted on 2006-05-23 19:37 junky 閱讀(259) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): spring

          主站蜘蛛池模板: 鱼台县| 新营市| 淮安市| 景宁| 郴州市| 苏尼特左旗| 昔阳县| 广东省| 芦溪县| 桦川县| 松江区| 清涧县| 禹城市| 南通市| 大化| 镇平县| 萝北县| 阿拉尔市| 镇安县| 原平市| 丰城市| 柞水县| 同江市| 竹溪县| 丰顺县| 四子王旗| 伊春市| 旌德县| 饶平县| 西畴县| 手游| 原平市| 饶阳县| 平度市| 阳西县| 彭泽县| 方正县| 聂荣县| 太康县| 喀喇沁旗| 汉中市|