【Spring入门】11.Bean继承
✅作者简介:正在学习java全栈,有兴趣的可以关注我一起学习
📃个人主页:ConderX(摸鱼)的主页
🔥系列专栏:Spring专栏
💖如果觉得博主的文章还不错的话,请👍三连支持一下博主哦🤞
Spring Bean继承
Spring Bean 的定义中可以包含很多配置信息,例如构造方法参数、属性值。子 Bean 既可以继承父 Bean 的配置数据,也可以根据需要重写或添加属于自己的配置信息。
继承方法
在Spring XML配置中,通过子Bean的parent属性来指定需要继承的父Bean
<bean id="parentBean" class="org.example.ParentBean" > <property name="stuId" value="1"></property> <property name="stuName" value="张三"></property></bean> <bean id="childBean" class="org.example.ChildBean" parent="parentBean"></bean>
代码示例:
-
Animal类(父bean)
package org.parentDemo;/ * 父Bean */public class Animal { private String name; private Integer age; @Override public String toString() { return "Animal{" + "name='" + name + '\'' + ", age=" + age + '}'; } public void setName(String name) { System.out.println("Animal setName:" + name); this.name = name; } public void setAge(Integer age) { System.out.println("Animal setAge:" + age); this.age = age; }}
-
Dog类(子bean)
package org.parentDemo;/ * 子Bean */public class Dog { private String name; private Integer age; private String call; @Override public String toString() { return "Dog{" + "name='" + name + '\'' + ", age=" + age + ", call='" + call + '\'' + '}'; } public void setName(String name) { System.out.println("Dog setName:" + name); this.name = name; } public void setAge(Integer age) { System.out.println("Dog setAge:" + age); this.age = age; } public void setCall(String call) { System.out.println("Dog setCall:" + call); this.call = call; }}
-
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-3.0.xsd"> <!--父Bean--> <bean id="animal" class="org.parentDemo.Animal"> <property name="name" value="动物"/> <property name="age" value="10"/> </bean> <!--子Bean--> <bean id="dog" class="org.parentDemo.Dog" parent="animal"> <property name="name" value="阿黄"/> <property name="age" value="12"/> <property name="call" value="汪汪汪!"/> </bean></beans>
-
Main方法
package org.parentDemo;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("parentBeans.xml"); Dog dog=(Dog) context.getBean("dog"); System.out.println(dog); }}
-
运行结果
Animal setName:动物Animal setAge:10 Dog setName:阿黄Dog setAge:12Dog setCall:汪汪汪!Dog{name='阿黄', age=12, call='汪汪汪!'}
Bean定义模板
如果父Bean的abstract属性值为true,则表明这个Bean是抽象的.这个抽象的父Bean只能作为模板被子Bean继承,不能被实例化,也不能被其他Bean引用,更不能在代码中根据getBean()方法获取,否则会返回错误。
在父Bean的定义中,可选择指定class属性。如果父Bean定义没有指定class属性,那么这个父Bean的abstract属性必须为true。
代码示例:
将parentBeans的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-3.0.xsd"> <!--父Bean--> <bean id="animal" abstract="true"> <property name="name" value="动物"/> <property name="age" value="10"/> </bean> <!--子Bean--> <bean id="dog" class="org.parentDemo.Dog" parent="animal"> <property name="name" value="阿黄"/> <property name="age" value="12"/> <property name="call" value="汪汪汪!"/> </bean></beans>
运行结果: 通过控制台输出可以看出,在 Spring 启动时,并没有实例化 animal。
Dog setName:阿黄Dog setAge:12Dog setCall:汪汪汪!Dog{name='阿黄', age=12, call='汪汪汪!'}