szhswl
          宋針還的個(gè)人空間

          姓名: 蔡超 生日: 1976-09-03 總結(jié): 六年大企業(yè)軟件開發(fā)經(jīng)驗(yàn)(其中兩年任軟件架構(gòu)師),精通J2EE相關(guān)技術(shù)及架構(gòu)設(shè)計(jì),是SUN及Microsoft培訓(xùn)中心特邀高端講師 SUN認(rèn)證的J2EE架構(gòu)師(SCEA) SUN認(rèn)證的商務(wù)組件開發(fā)員(SCBCD) IBM認(rèn)證的RUP專家 IBM認(rèn)證的OOA&D (UML v2)專家




          對(duì)于annotation的支持和組件的配置方式是擴(kuò)充是spring2.5的主要提升之一。其中組件在類路徑下的自動(dòng)搜索功能也是一項(xiàng)值得注意的新增特性。
          現(xiàn)來(lái)看個(gè)示例:

          圖表 1 示例中涉及的類的關(guān)系
          Command.java:
          package org.ccsoft.commands.imp;

          public interface Command {
          public void execute();
          }

          CommandExecuter.java:
          package org.ccsoft.commands.imp;

          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.beans.factory.annotation.Qualifier;
          import org.springframework.stereotype.Component;
          @Component("commandExecuter")
          public class CommandExecuter {
          private Command command;
          @Autowired

          public void setCommand(/*@Qualifier("helloCommand")*/ Command command) {
          this.command = command;
          }
          public void executeCommand(){
          command.execute();
          }
          }

          HelloCommand.java
          package org.ccsoft.commands.imp;

          import org.springframework.stereotype.Component;


          @Component
          public class HelloCommand implements Command{
          public void execute() {
          System.out.println("Hello World");
          }
          }

          配置文件:
          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"" target="_new">http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:context="http://www.springframework.org/schema/context"" target="_new">http://www.springframework.org/schema/context"
          xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-2.5.xsd">

          <context:component-scan base-package="org.ccsoft"/>
          </beans>

          測(cè)試程序如下:
          package org.ccsoft;

          import org.ccsoft.commands.imp.CommandExecuter;
          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;

          import junit.framework.TestCase;

          public class TestCommandExecuter extends TestCase {
          public void testExecuteCommand(){
          ApplicationContext ctx=new ClassPathXmlApplicationContext("application.xml");
          CommandExecuter exe=(CommandExecuter) ctx.getBean("commandExecuter");
          exe.executeCommand();
          }
          }
          可以從配置文件中發(fā)現(xiàn),這次并沒有像常規(guī)的方式那樣,聲明所使用的bean,只是說(shuō)明了組件搜索的基路徑(便于減少搜索范圍)“<context:component-scan base-package="org.ccsoft"/>”。
          要點(diǎn):
          1 用@Component來(lái)聲明每個(gè)組件,而不是通過(guò)配置文件中的bean元素
          2 在需要注入其他組件的地方使用@Autowired,默認(rèn)情況下會(huì)按類型進(jìn)行依賴關(guān)系的查找和注入
          3 可以指定組件的Id,通過(guò)@Component的值,如:@Component("commandExecuter"),這樣類似于<bean id=” commandExecuter”…,注意如果不注明該值,spring將使用默認(rèn)的命名規(guī)則,即類的名字但首字母小寫,所以在上例中將@Component("commandExecuter")改寫為@Component同樣是可以運(yùn)行的。

          代碼試驗(yàn)二:
          在上面工程中加入新的Command接口實(shí)現(xiàn)類SorryCommand.
          package org.ccsoft.commands.imp;

          import org.springframework.stereotype.Component;
          @Component
          public class SorryCommand implements Command {

          public void execute() {
          // TODO Auto-generated method stub
          System.out.println("Sorry");
          }

          }
          再運(yùn)行測(cè)試程序,會(huì)發(fā)現(xiàn)程序無(wú)法運(yùn)行,拋出以下異常org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.ccsoft.commands.imp.Command] is defined: expected single matching bean but found 2: [helloCommand, sorryCommand]
          這是因?yàn)楦鶕?jù)類型的方式搜索,找到了兩個(gè)相符的依賴類,這是我們必須指明使用那個(gè)組件。
          @Autowired

          public void setCommand(@Qualifier("helloCommand") Command command) {
          this.command = command;
          }
          要點(diǎn):
          4 在多個(gè)同類組件存在于查找路徑時(shí)必須通過(guò)@Qualifier指明

          不知為什么,spring的這一新特性總讓我聯(lián)想起OSGi DS,不過(guò)與OSGi DS相比個(gè)人認(rèn)為spring的IoC還是有些不便的地方,OSGi DS的Service的引用策略中的Cardinality可以指定組件的注入策略,可以支持將多個(gè)符合條件的組件多次注入,這為編寫組件容器這樣的應(yīng)用提供了很大的便利。
          如:

          private List<Command> commands;

          public void setCommand(Command cmd){
          commands.add(cmd);
          }

          ….

          最后,這里只是涉及了spring2.5中相關(guān)內(nèi)容中非常少的一點(diǎn),如果大家有興趣深入了解可以參考spring reference。
          個(gè)人認(rèn)為在大家充分享受新特性帶來(lái)的便利的同時(shí)也要主要到新特性是否會(huì)對(duì)應(yīng)用的可維護(hù)性和部署時(shí)改變注入關(guān)系的方便性帶來(lái)負(fù)面影響。



          蔡超
          JavaEE 咨詢顧問
          SCEA
          IBM Certified Solution Designer for OOA&D UML2

          轉(zhuǎn)自:http://dev2dev.bea.com.cn/blog/chaocai/200712/spring_osgi_04_719.html



          ---------------------------------------------------------------------------------------------------------------------------------
          說(shuō)人之短,乃護(hù)己之短。夸己之長(zhǎng),乃忌人之長(zhǎng)。皆由存心不厚,識(shí)量太狹耳。能去此弊,可以進(jìn)德,可以遠(yuǎn)怨。
          http://www.aygfsteel.com/szhswl
          ------------------------------------------------------------------------------------------------------ ----------------- ---------
          posted on 2007-12-05 09:59 宋針還 閱讀(788) 評(píng)論(0)  編輯  收藏 所屬分類: SPRING

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 西昌市| 鹿泉市| 三穗县| 金川县| 泌阳县| 噶尔县| 崇义县| 禹州市| 龙游县| 即墨市| 通榆县| 景泰县| 丹棱县| 乐清市| 江安县| 健康| 新龙县| 磴口县| 定安县| 瓦房店市| 杭州市| 保山市| 湖北省| 茂名市| 志丹县| 福安市| 清镇市| 陇南市| 临江市| 永泰县| 齐齐哈尔市| 梁山县| 松原市| 永和县| 柳林县| 米泉市| 沐川县| 高邮市| 贺兰县| 马鞍山市| 金山区|