Bean定义继承:Spring bean定义的继承与java类无关,bean定义继承指的是子bean继承父bean的配置数据,即定义一个父bean作为模板,其他bean可以从父bean中继承所需的配置,也可以重写或添加其他值。
新建一个测试继承的工程,配置文件Beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置bean -->
<bean id="helloWorld" class="com.atguigu.spring.beans.HelloWorld">
<property name="message1" value="Hello World!"></property>
<property name="message2" value="Hello World Secondly!"></property>
</bean>
<bean id="helloIndia" class="com.atguigu.spring.beans.HelloIndia" parent="helloWorld">
<property name="message1" value="Hello India!"></property>
<property name="message3" value="Hello India Secondly!"></property>
</bean>
</beans>
HelloWorld.java的代码:
package com.atguigu.spring.beans;
public class HelloWorld {
private String message1;
private String message2;
public void setMessage1(String message){
this.message1 = message;
}
public void setMessage2(String message){
this.message2 = message;
}
public void getMessage1(){
System.out.println("World Message1 : " + message1);
}
public void getMessage2(){
System.out.println("World Message2 : " + message2);
}
}
HelloIndia.java 文件的内容:
package com.atguigu.spring.beans;
public class HelloIndia {
private String message1;
private String message2;
private String message3;
public void setMessage1(String message){
this.message1 = message;
}
public void setMessage2(String message){
this.message2 = message;
}
public void setMessage3(String message){
this.message3 = message;
}
public void getMessage1(){
System.out.println("India Message1 : " + message1);
}
public void getMessage2(){
System.out.println("India Message2 : " + message2);
}
public void getMessage3(){
System.out.println("India Message3 : " + message3);
}
}
Main.java的内容如下:
package com.atguigu.spring.beans;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
objA.getMessage1();
objA.getMessage2();
HelloIndia objB = (HelloIndia) context.getBean("helloIndia");
objB.getMessage1();
objB.getMessage2();
objB.getMessage3();
}
}
正常输出结果如下:
这里Bean.xml文件中并没有配置HelloIndia的message2,但是结果输出却有,这就是Spring Bean继承的影响。
完整代码:
Bean定义模板
可以创建一个 Bean 定义模板,不需要花太多功夫它就可以被其他子 bean 定义使用。在定义一个 Bean 定义模板时,你不应该指定类的属性,而应该指定带 true 值的抽象属性,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置bean -->
<bean id="helloWorld" abstract="true">
<property name="message1" value="Hello World!"></property>
<property name="message2" value="Hello World Secondly!"></property>
</bean>
<bean id="helloIndia" class="com.atguigu.spring.beans.HelloIndia" parent="helloWorld">
<property name="message1" value="Hello India!"></property>
<property name="message3" value="Hello India Secondly!"></property>
</bean>
</beans>
当bean定义成bean模板后,父类就是抽象的,不可以实例化,只作为子bean的继承bean使用,子bean可以重写和继承父bean。