ApplicationContext詳解以及多個ApplicationContext.xml的相互引用

來源:互聯網
上載者:User

標籤:優先   beans   ati   files   .com   nbsp   負載檔案   ges   web.xml   

如果說BeanFactory是spring的心臟,那麼Application就是完整的身軀。ApplicationContext就是由BeanFactory派生出來的。

一、ApplicationContext類

 ApplicationContext的主要實作類別是ClassPathXmlApplicationContext和FileSystemXmlApplicationContext,前者預設從類路徑載入配置  檔案,後者預設  從檔案系統負載檔案。

1)如果設定檔放在類路徑下,直接使用ClassPathXmlApplicationContext實作類別:

   ApplicationContext ctx=new ClassPathXmlApplicationContext("com/techman/context/beans.xml");

   這裡的參數等同於:"classpath:com/techman/context/beans.xml"

2)如果設定檔在檔案系統的路徑下,則可以優先考慮使用FileSystemXmlApplicationContext實作類別:

   ApplicationContext ctx=new FileSystemXmlApplicationContext("com/techman/context/beans.xml");

   這裡的參數等同於:"file:com/techman/context/beans.xml".

3)如果有多個設定檔,擷取ApplicationContext對象時還可以指定一組設定檔,Spring自動將多個設定檔在記憶體中整合成一個設定檔:

  ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"conf/bean1.xml","conf/bean2.xml"});

或者ApplicationContext ctx=new ClassPathXmlApplicationContext("conf/bean*.xml");

 

二、如何配置多個applicationContext.xml?

    即,在啟動容器的時候如何引入多個applicationContext.xml?

    在web.xml中將applicationContext.xml作為當前web應用的初始化參數,多個applicationContext.xml用逗號隔開,也可以使用

    例如:

   

    或者

 

三、一個applicationContext.xml檔案如何引用另一個applicationContext.xml檔案中的bean?

    在需要引用其它applicationContext.xml檔案的applicationContext.xml檔案中的<beans></beans>標記之間引入別人的            

    applicationContext.xml,格式如下:

    <beans>
           <import resource="applicationContext-hibernate.xml"/>
    </beans>
    (其中,<inport> 相當於Java中的導包)
    需要注意的是:
    <import resource="applicationContext-hibernatee.xml"/>這一句要放在所有bean配置的最前面。
   
四、如何引用別人已經配置好了的bean呢?
    例如,將另外一個人配置的commonDAO注入給自己的biz中:配置如下:
   <bean id="cstServiceBiz" class="org.jb.t0821c.biz.CstServiceBiz">
          <property name="commonDAO">
               <ref bean="commonDAO"/>
          </property>
   </bean>
  

ApplicationContext詳解以及多個ApplicationContext.xml的相互引用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.