- Spring核心基础
- Spring - 首页
- Spring - 概述
- Spring - 架构
- Spring - 环境设置
- Spring - Hello World 示例
- Spring - IoC 容器
- Spring - Bean 定义
- Spring - Bean 作用域
- Spring - Bean 生命周期
- Spring - Bean 后置处理器
- Spring - Bean 定义继承
- Spring - 依赖注入
- Spring - 注入内部 Bean
- Spring - 注入集合
- Spring - Bean 自动装配
- 基于注解的配置
- Spring - 基于 Java 的配置
- Spring - Spring 中的事件处理
- Spring - Spring 中的自定义事件
- Spring - 使用 Spring 框架进行 AOP
- Spring - JDBC 框架
- Spring - 事务管理
- Spring - Web MVC 框架
- Spring - 使用 Log4J 进行日志记录
- Spring 问题及解答
- Spring - 问题及解答
- Spring 有用资源
- Spring - 快速指南
- Spring - 有用资源
- Spring - 讨论
Spring - Bean 定义继承
Bean 定义可以包含许多配置信息,包括构造函数参数、属性值以及容器特定的信息,例如初始化方法、静态工厂方法名称等等。
子 Bean 定义继承父定义的配置数据。子定义可以根据需要覆盖某些值或添加其他值。
Spring Bean 定义继承与 Java 类继承无关,但继承的概念相同。您可以将父 Bean 定义定义为模板,其他子 Bean 可以从父 Bean 继承所需的配置。
当您使用基于 XML 的配置元数据时,您可以使用parent属性指示子 Bean 定义,并将父 Bean 指定为此属性的值。
示例
让我们准备一个可用的 Eclipse IDE,并采取以下步骤创建 Spring 应用程序:
| 步骤 | 描述 |
|---|---|
| 1 | 创建一个名为SpringExample的项目,并在创建的项目中src文件夹下创建一个com.tutorialspoint包。 |
| 2 | 使用添加外部 JAR选项添加所需的 Spring 库,如Spring Hello World 示例章节中所述。 |
| 3 | 在com.tutorialspoint包下创建 Java 类HelloWorld、HelloIndia和MainApp。 |
| 4 | 在src文件夹下创建 Bean 配置文件Beans.xml。 |
| 5 | 最后一步是创建所有 Java 文件和 Bean 配置文件的内容,并按以下说明运行应用程序。 |
以下是配置文件Beans.xml,其中我们定义了“helloWorld” Bean,它具有两个属性message1和message2。接下来,“helloIndia” Bean 被定义为“helloWorld” Bean 的子 Bean,使用parent属性。子 Bean 按原样继承message2属性,覆盖message1属性并引入另一个属性message3。
<?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-3.0.xsd">
<bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld">
<property name = "message1" value = "Hello World!"/>
<property name = "message2" value = "Hello Second World!"/>
</bean>
<bean id ="helloIndia" class = "com.tutorialspoint.HelloIndia" parent = "helloWorld">
<property name = "message1" value = "Hello India!"/>
<property name = "message3" value = "Namaste India!"/>
</bean>
</beans>
以下是HelloWorld.java文件的内容:
package com.tutorialspoint;
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.tutorialspoint;
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);
}
}
以下是MainApp.java文件的内容:
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
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 配置文件后,让我们运行应用程序。如果您的应用程序一切正常,它将打印以下消息:
World Message1 : Hello World! World Message2 : Hello Second World! India Message1 : Hello India! India Message2 : Hello Second World! India Message3 : Namaste India!
如果您观察到,我们在创建“helloIndia” Bean 时没有传递 message2,但由于 Bean 定义继承,它被传递了。
Bean 定义模板
您可以创建一个 Bean 定义模板,其他子 Bean 定义可以使用它,而无需付出太多努力。在定义 Bean 定义模板时,您不应指定class属性,而应指定abstract属性,并应将 abstract 属性的值指定为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-3.0.xsd">
<bean id = "beanTeamplate" abstract = "true">
<property name = "message1" value = "Hello World!"/>
<property name = "message2" value = "Hello Second World!"/>
<property name = "message3" value = "Namaste India!"/>
</bean>
<bean id = "helloIndia" class = "com.tutorialspoint.HelloIndia" parent = "beanTeamplate">
<property name = "message1" value = "Hello India!"/>
<property name = "message3" value = "Namaste India!"/>
</bean>
</beans>
父 Bean 本身无法实例化,因为它不完整,并且还明确标记为abstract。当定义像这样是抽象的时,它只能用作纯模板 Bean 定义,作为子定义的父定义。