[译]11-spring bean定义的继承
spring中bean的定义包含很多信息,如,构造器参数、property指定的依赖项、初始化方法、工厂类和工厂方法等.
如果spring容器的中每个bean都重复声明这些属性,是非常烦人也是十分低效易出错的.好在spring的bean定义可
以继承.
一个子的bean定义可以从一个父bean定义中继承得到所有的属性,并且子bean的定义可以覆盖其通过继承得来的
父bean定义的属性.
可以在一个bean的定义中使用parent属性指定其需要继承的bean定义.来看个例子:
1.新建包com.tutorialspoint.bean_inherit,并在包中新建HelloWorld和HelloIndia类.内容分别如下:
HelloWorld.java如下:
package com.tutorialspoint.bean_inherit; 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.bean_inherit; 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); } }
2.在src目录下新建bean_inherits.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 id="helloWorld" class="com.tutorialspoint.bean_inherit.HelloWorld"> <property name="message1" value="Hello World!"/> <property name="message2" value="Hello Second World!"/> </bean> <!-- 通过parent属性指定需要继承的bean的定义 --> <!-- 覆盖了parent的message1属性 --> <!-- 继承了parent的message2属性 --> <!-- 新加了helloIndia的message3属性 --> <bean id="helloIndia" class="com.tutorialspoint.bean_inherit.HelloIndia" parent="helloWorld"> <property name="message1" value="Hello India!"/> <property name="message3" value="Namaste India!"/> </bean> </beans>
3.在包com.tutorialspoint.bean_inherit中新建MainApp.java类,内容如下:
package com.tutorialspoint.bean_inherit; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("bean_inherits.xml"); HelloWorld objA = (HelloWorld) context.getBean("helloWorld"); objA.getMessage1(); objA.getMessage2(); HelloIndia objB = (HelloIndia) context.getBean("helloIndia"); objB.getMessage1(); objB.getMessage2(); objB.getMessage3(); context.registerShutdownHook(); } }
4.运行代码,检查结果:
bean定义的模版
上述如果我们仅需要使用HelloIndia类,单纯为了继承而定义HelloWorld类是比较繁琐的.好在spring支持bean定义的模板
.bean定义的模板就是把<bean/>元素中添加abstract="true"属性,而不必指定class属性.其他的bean直接从bean模板继
承。我们把上述代码的配置文件bean_inherits.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 id="beanTeamplate" abstract="true"> <property name="message1" value="Hello World!"/> <property name="message2" value="Hello Second World!"/> <property name="message3" value="Namaste India!"/> </bean> <!-- 通过parent属性指定需要继承的bean定义的模板--> <!-- 覆盖了parent的message1属性 --> <!-- 继承了parent的message2属性 --> <!-- 继承了parent的message3属性 --> <bean id="helloIndia" class="com.tutorialspoint.bean_inherit.HelloIndia" parent="beanTeamplate"> <property name="message1" value="Hello India!"/> </bean> </beans>
并把MainApp.java修改成如下内容:
package com.tutorialspoint.bean_inherit; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("bean_inherits.xml"); HelloIndia objB = (HelloIndia) context.getBean("helloIndia"); objB.getMessage1(); objB.getMessage2(); objB.getMessage3(); context.registerShutdownHook(); } }
继续运行MainApp.java类,检查结果,可以看到已经正常运行:
分类:
Spring
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· 几个技巧,教你去除文章的 AI 味!
· 系统高可用的 10 条军规
· 对象命名为何需要避免'-er'和'-or'后缀
· 关于普通程序员该如何参与AI学习的三个建议以及自己的实践
· AI与.NET技术实操系列(八):使用Catalyst进行自然语言处理