spring筆記——ref屬性的設定__spring

來源:互聯網
上載者:User
用ref的屬性指定依賴的3種模式 local\bean\parent
local

如果一個bean與被參考引用的bean在同一個xml 檔案中而且被引用參考的bean是用id來命名的,那麼就可以使用ref的local屬性。這樣會讓項目裡解析器更早的在xml文檔解析時,驗證bean的id。

<bean id="signle" class="com.st.sig.Single" autowire="default" >
  <property name="name" >
  <ref local="mege"/>
  </property>
  </bean>
  <bean id="mege" name="mege" class="com.st.sig.Mege" ></bean>
bean

用ref元素的bean屬性指定被參考引用的bean是spring中最常見的形式,它允許指向的bean可以在同一個xml,也可以不在同一個xml中。bean屬性的值可以與被參考引用的bean的id屬性相同,也可以與被參考引用的bean的那麼屬性相同。

<bean id="signle" class="com.st.sig.Single" autowire="default" >
  <property name="name" >
  <ref bean="mege"/>
  </property>
  </bean>
  <bean id="mege" name="mege" class="com.st.sig.Mege" ></bean>

parent

用parent屬性指定被參考引用的bean時,允許引用當前beanfactory或applicationContext的父beanfactory或者applicationcontext中的bean,parent屬性的值可以和被參考引用的bean的name相同也可以和被參考引用的bean的id相同

<bean id="signle" class="com.st.sig.Single" autowire="default" >
   <property name="name" >
   <ref parent="mege"/>
   </property>
   </bean>
   <bean id="mege" name="mege" class="com.st.sig.Mege" ></bean>

bean和local的相同點:

bean和id都可以使用bean的id參考引用,都可以對同一xml中的bean進行參考引用

bean和local的不同點:
用bean屬性指定依賴可以使用bean的name來進行參考引用,還可以對不同xml中的bean進行參考引用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.