Spring配置中 bean上的屬性parent的作用_SPRING

來源:互聯網
上載者:User

省去多餘的父類配置,比如 交易管理:

<bean id="basicTxProxy2" abstract="true"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"><property name="transactionManager" ref="transactionManager2" /><property name="transactionAttributes"><props><prop key="save*">PROPAGATION_REQUIRED</prop><prop key="add*">PROPAGATION_REQUIRED</prop><prop key="remove*">PROPAGATION_REQUIRED</prop><prop key="update*">PROPAGATION_REQUIRED</prop><prop key="*">PROPAGATION_REQUIRED</prop></props></property></bean>

<bean id="torganRelationService" parent="basicTxProxy"><property name="target" ref="torganRelationServiceImpl" /></bean>

由於聲明時沒有target屬性,所以必須聲明為抽象類別,即abstract = true;

注意

預設情況下,
ApplicationContext(不是BeanFactory)會預執行個體化所有singleton的bean。因此很重要的一點是:如果你只想把一個(父)bean定義當作模板使用,而它又指定了class屬性,那麼你就得將'abstract'屬性設定為'true',否則應用上下文將會(試著)預執行個體化
抽象
bean。 註:由於設定bean定義中設定了abstract="true",因此它不能被容器執行個體化,只是在此起了模板的作用,供其他bean繼承,所以在它的屬性在類體中可以不定義,直接在bean的聲明中以<proerty/>聲明即可。子bean繼承他後需要在提供對應的屬性和set方法即可,在子bean中就可擷取從父bean繼承來的值

如果子bean定義沒有指定class屬性,它將使用父bean定義的class屬性,當然也可以覆蓋它。在後面一種情況中,子bean的class屬性值必須同父bean相容,也就是說它必須接受父bean的屬性值。

一個子bean定義可以從父bean繼承構造器參數值、屬性值以及覆蓋父bean的方法,並且可以有選擇地增加新的值。如果指定了init-method,destroy-method和/或靜態factory-method,它們就會覆蓋父bean相應的設定。

剩餘的設定將總是從子bean定義處得到:依賴、自動裝配模式、依賴檢查、singleton、範圍和延遲初始化。

下面是一般性的例子:

父類:

<bean id="abstractServiceThread" class="com.project.schedual.ServiceThread" abstract="true"><property name="baseDao" ref="baseDAO"></property></bean>

子類:

<!-- zhoushun 2級流程 --><bean id="docReceiveFlowThread" parent="abstractServiceThread"><property name="svc" ref="docReceiveFlowService"></property></bean>

因為子類自動繼承父類的屬性,所以 svc 注入的是 父類class 中的屬性:

<beanid="docReceiveFlowServices"class="com.project.docReceiveFlow.service.DocReceiveFlowService"><property name="baseDao" ref="baseDAO"/></bean><beanid="docReceiveFlowService" parent="basicTxProxy"><property name="proxyTargetClass" value="true"/>  <property name="target" ref="docReceiveFlowServices"/></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.