从0到1学J2EE第十三天——Spring中bean的继承

By | 12月 31, 2017

1.bean的继承是什么?

bean的继承是一个或者多个bean获得另外一个bean的值、属性等,子bean可以覆盖父bean的值。

2.为什么要进行bean的继承?

通过继承可以将共用的属性及值放到同一个父Bean,可以减少代码量,提高效率,在避免多个Bean有重复共同的值或配置方面非常有用。

3.如何实现bean的继承?

3.1正常继承

正常继承,父bean可以实例化,意思即为子bean可以覆盖父bean的值。ozdata是父bean,ozdatajc是子bean,ozdatajc从ozdata那里继承了name属性和值“一零”,因此在输出结果时通过调用子bean即实现了所有信息的输出。

inConfig.xml  配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
    <bean id="ozdata" class="pers.onezero.jicheng.ozJich">
    	<property name="name" value="一零"/>
    </bean>
    <bean id="ozdatajc" parent="ozdata" >
    	<property name="year" value="2017" />
    	<property name="job" value="JAVA开发工程师"/>
    </bean>
</beans>

输出结果如下:

3.2抽象继承

继承抽象,基础模板不能实例化,实现该效果需要在bean中添加属性abstract=”true”。如果要对bean进行实例化,在运行时则会提示报错!

inConfigAbstract.xml 

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
    <bean id="ozdataAb" class="pers.onezero.jicheng.ozJich" abstract="true">
    	<property name="name" value="零一"/>
    </bean>
    <bean id="ozdatajcAb" parent="ozdataAb">
    	<property name="year" value="2018"/>
    	<property name="job" value="JAVA全栈开发工程师"/>
    </bean>
</beans>

执行结果:

错误的实例化操作:

ozJich jich = (ozJich)context.getBean("ozdataAb");
jich.printJich();

此时指向的bean是抽象设置的父bean,会出现如下错误提示:

Exception in thread "main" org.springframework.beans.factory.BeanIsAbstractException: Error creating bean with name 'ozdatajcAb': Bean definition is abstract
	at org.springframework.beans.factory.support.AbstractBeanFactory.checkMergedBeanDefinition(AbstractBeanFactory.java:1271)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:281)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
	at pers.onezero.jicheng.testJich.main(testJich.java:17)

3.3纯继承模板

纯继承模板,目的是实现相同属性共享。以上三种方式都可以实现子bean属性对父bean属性的覆盖。

inConfigChunjc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
    <bean id="ozdataChun" abstract="true">
    	<property name="name" value="零零一" />
    </bean>
    <bean id="ozdatajcChun" parent="ozdataChun" class="pers.onezero.jicheng.ozJich" 
       init-method="printJich" destroy-method="cleanUp">
    	<property name="year" value="2018"/>
    	<property name="job" value="JAVA架构师"/>
    </bean>
</beans>

执行结果:

注:

a.在本次中用了多个xml配置文件,因此注意利用一个主配置文件将其余文件引入。

BeanConfig.xml 主配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
	
    <import resource="inConfig.xml"/>
    <import resource="inConfigAbstract.xml" />
    <import resource="inConfigChunjc.xml"/>
</beans>

b.本次文件的目录结构如下,ozjcInter.java是接口文件,ozJich是主文件,testJich是执行文件。

c.本文的相关源码的github地址

发表评论

您的电子邮箱地址不会被公开。