<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    J2EE之巔

     

    Spring2.5的組件自動搜索

    http://dev2dev.bea.com.cn/blog/chaocai/200712/spring_osgi_04_719.html

    posted on 2007-12-04 12:50 超越巔峰 閱讀(2734) 評論(1)  編輯  收藏 所屬分類: Java EE

    評論

    # re: Spring2.5的組件自動搜索 2007-12-04 19:24 BeanSoft

    姓名: 蔡超 生日: 1976-09-03 總結: 六年大企業軟件開發經驗(其中兩年任軟件架構師),精通J2EE相關技術及架構設計,是SUN及Microsoft培訓中心特邀高端講師 SUN認證的J2EE架構師(SCEA) SUN認證的商務組件開發員(SCBCD) IBM認證的RUP專家 IBM認證的OOA&D (UML v2)專家




    對于annotation的支持和組件的配置方式是擴充是spring2.5的主要提升之一。其中組件在類路徑下的自動搜索功能也是一項值得注意的新增特性。
    現來看個示例:

    圖表 1 示例中涉及的類的關系
    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>

    測試程序如下:
    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();
    }
    }
    可以從配置文件中發現,這次并沒有像常規的方式那樣,聲明所使用的bean,只是說明了組件搜索的基路徑(便于減少搜索范圍)“<context:component-scan base-package="org.ccsoft"/>”。
    要點:
    1 用@Component來聲明每個組件,而不是通過配置文件中的bean元素
    2 在需要注入其他組件的地方使用@Autowired,默認情況下會按類型進行依賴關系的查找和注入
    3 可以指定組件的Id,通過@Component的值,如:@Component("commandExecuter"),這樣類似于<bean id=” commandExecuter”…,注意如果不注明該值,spring將使用默認的命名規則,即類的名字但首字母小寫,所以在上例中將@Component("commandExecuter")改寫為@Component同樣是可以運行的。

    代碼試驗二:
    在上面工程中加入新的Command接口實現類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");
    }

    }
    再運行測試程序,會發現程序無法運行,拋出以下異常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]
    這是因為根據類型的方式搜索,找到了兩個相符的依賴類,這是我們必須指明使用那個組件。
    @Autowired

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

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

    private List<Command> commands;

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

    ….

    最后,這里只是涉及了spring2.5中相關內容中非常少的一點,如果大家有興趣深入了解可以參考spring reference。
    個人認為在大家充分享受新特性帶來的便利的同時也要主要到新特性是否會對應用的可維護性和部署時改變注入關系的方便性帶來負面影響。



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




    Tags: Spring OSGi

      回復  更多評論   

    導航

    統計

    常用鏈接

    留言簿(12)

    隨筆分類(54)

    隨筆檔案(59)

    文章分類(2)

    文章檔案(1)

    相冊

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 久久亚洲sm情趣捆绑调教| 一级毛片在线免费观看| 亚洲第一永久在线观看| 区久久AAA片69亚洲| 日韩在线天堂免费观看| 7x7x7x免费在线观看| 日韩免费高清播放器| 国产中文字幕免费观看| 成年人免费视频观看| 免费在线视频你懂的| 久久免费视频99| 国产精品一区二区三区免费| 亚洲av无码兔费综合| 亚洲另类无码专区首页| 亚洲国产精品专区| 亚洲无限乱码一二三四区| 亚洲AV日韩精品久久久久久| 夜夜春亚洲嫩草影院| 亚洲毛片网址在线观看中文字幕 | 亚洲第一视频网站| 久久乐国产精品亚洲综合| 国产公开免费人成视频| 在线A级毛片无码免费真人 | 亚洲乱妇熟女爽到高潮的片| 亚洲av无码国产综合专区| 亚洲视频手机在线| 91在线精品亚洲一区二区| 7777久久亚洲中文字幕蜜桃| 亚洲免费视频在线观看| 久久精品国产亚洲AV麻豆不卡| 久久九九亚洲精品| 亚洲V无码一区二区三区四区观看| 亚洲无人区午夜福利码高清完整版 | 亚洲国产婷婷香蕉久久久久久| 免费国产成人高清在线观看麻豆| 国产精品国产自线拍免费软件| 日本a级片免费看| www.亚洲色图.com| 亚洲一区精品伊人久久伊人| 亚洲一区二区三区影院 | 免费一级不卡毛片|