當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring bean配置继承
生活随笔
收集整理的這篇文章主要介紹了
Spring bean配置继承
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在 Spring,繼承是用為支持bean設置一個 bean 來分享共同的值,屬性或配置。 一個子 bean 或繼承的bean可以繼承其父 bean 的配置,屬性和一些屬性。另外,子 Bean 允許覆蓋繼承的值。 請參見下面的完整的例子來告訴你如何配置 bean 繼承在 Spring 中工作。 package com.yiibai.common;public class Customer {private int type;private String action;private String Country;//...}
Bean配置文件 <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="BaseCustomerMalaysia" class="com.yiibai.common.Customer"><property name="country" value="Malaysia" /></bean><bean id="CustomerBean" parent="BaseCustomerMalaysia"><property name="action" value="buy" /><property name="type" value="1" /></bean></beans>
以上就是“BaseCustomerMalaysia”?Bean中含有的 country?屬性的值,而“CustomerBean”?Bean?繼承其父('BaseCustomerMalaysia')這個值。
執行它
package com.yiibai.common;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {public static void main( String[] args ){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Customer cust = (Customer)context.getBean("CustomerBean");System.out.println(cust);} }輸出結果
Customer [type=1, action=buy, Country=Malaysia] CustomerBean?Bean 只從它的父(“BaseCustomerMalaysia”)繼承?country?屬性。 繼承抽象 在上面的例子中,'BaseCustomerMalaysia' 仍然能夠實例化,例如, Customer cust = (Customer)context.getBean("BaseCustomerMalaysia"); 如果你要讓這個?bean?作為一個基礎模板,不允許別人來實例化它,可以在一個<bean>元素中添加一個“abstract”的屬性。?例如 <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="BaseCustomerMalaysia" class="com.yiibai.common.Customer" abstract="true"><property name="country" value="Malaysia" /></bean><bean id="CustomerBean" parent="BaseCustomerMalaysia"><property name="action" value="buy" /><property name="type" value="1" /></bean></beans> 現在,“BaseCustomerMalaysia'?Bean是一個純粹的模板,因為Bean只能繼承它,如果試圖實例化它,你會遇到以下錯誤消息。 Customer cust = (Customer)context.getBean("BaseCustomerMalaysia"); org.springframework.beans.factory.BeanIsAbstractException: Error creating bean with name 'BaseCustomerMalaysia': Bean definition is abstract 純繼承模板 其實,父 bean 是不需要定義類的屬性,很多時候,你可能只需要一個共同的屬性共享。這里的是一個例子 <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="BaseCustomerMalaysia" abstract="true"><property name="country" value="Malaysia" /></bean><bean id="CustomerBean" parent="BaseCustomerMalaysia" class="com.yiibai.common.Customer"><property name="action" value="buy" /><property name="type" value="1" /></bean></beans> 在這種情況下,“BaseCustomerMalaysia' Bean 是一個純粹的模板,只分享其 ”country“屬性。 覆蓋它 但是,仍然可以指定的子bean的新值覆蓋繼承的值。讓我們來看看這個例子 <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="BaseCustomerMalaysia" class="com.yiibai.common.Customer" abstract="true"><property name="country" value="Malaysia" /></bean><bean id="CustomerBean" parent="BaseCustomerMalaysia"><property name="country" value="Japan" /><property name="action" value="buy" /><property name="type" value="1" /></bean></beans> 在“CustomerBean”?Bean只是覆蓋父(“BaseCustomerMalaysia”)country?屬性,從?‘Malaysia’?修改為?‘Japan’. Customer [Country=Japan, action=buy, type=1]總結
Spring?bean配置繼承是為了避免多個Bean有重復共同的值或配置是非常有用的。 下載代碼 –?http://pan.baidu.com/s/1blOgAq http://www.yiibai.com/spring/spring-bean-configuration-inheritance.html總結
以上是生活随笔為你收集整理的Spring bean配置继承的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring注入日期到bean属性-Cu
- 下一篇: 黑旋风李逵(说一说黑旋风李逵的简介)