Bean之间的关系:继承和依赖。

继承Bean配置

Spring允许继承bean的配置,被继承的bean称为父bean,继承这个父bean的Bean称为子Bean。

子Bean从父Bean中继承配置,包括Bean的属性配置。

子Bean也可以覆盖从父Bean继承过来的配置。

父Bean可以作为配置模板,也可以作为Bean实例,若只想把父Bean作为模板,可以设置<bean>的abstract属性为true,这样Spring将不会实例化这个bean。(抽象Bean,抽象Bean不能被实例化,只能被继承)

并不是<bean>元素里的所有属性都会被继承,比如:autowire,abstract等。

也可以忽略父Bean的class属性,让子Bean指定自己的类,而共享相同的属性配置。但此时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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="address" class="logan.spring.study.autowire.Address"
p:city="Beijing" p:street="WuDaoKou"></bean> <bean id="address2" class="logan.spring.study.autowire.Address"
p:city="Beijing" p:street="DaZhongSi"></bean> </beans>
package logan.spring.study.relation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import logan.spring.study.autowire.Address; public class Main { public static void main(String[] args) {
// TODO Auto-generated method stub ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-relation.xml");
Address address = (Address) ctx.getBean("address");
System.out.println(address); address = (Address) ctx.getBean("address2");
System.out.println(address); } }

下面是输出结果:

五月 20, 2017 4:03:58 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3eb07fd3: startup date [Sat May 20 16:03:58 CST 2017]; root of context hierarchy
五月 20, 2017 4:03:58 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-relation.xml]
Address [city=Beijing, street=WuDaoKou]
Address [city=Beijing, street=DaZhongSi]

可以看到两个address的内容大部分都是一样的,所以,我们可以如下使用关系,让第二个address继承第一个address

如下配置

<?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.xsd"> <bean id="address" class="logan.spring.study.autowire.Address"
p:city="Beijing1" p:street="WuDaoKou"></bean> <bean id="address2" p:street="DaZhongSi" parent="address"></bean> </beans>

下面是输出结果

五月 20, 2017 4:08:07 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3eb07fd3: startup date [Sat May 20 16:08:07 CST 2017]; root of context hierarchy
五月 20, 2017 4:08:07 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-relation.xml]
Address [city=Beijing1, street=WuDaoKou]
Address [city=Beijing1, street=DaZhongSi]

下面看Bean之间的依赖关系

Spring允许用户通过depends-on属性设定Bean前置依赖的Bean,前置依赖的Bean会在本Bean实例化之前创建好。

如果前置依赖于多个Bean,则可以通过都好,空格的方式配置Bean的名称。

<?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.xsd"> <bean id="address" class="logan.spring.study.autowire.Address"
p:city="Beijing1" p:street="WuDaoKou"></bean> <bean id="address2" p:street="DaZhongSi" parent="address"></bean> <bean id="person" class="logan.spring.study.autowire.Person"
p:name="Tom" p:address-ref="address"></bean> </beans>
package logan.spring.study.relation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import logan.spring.study.autowire.Address;
import logan.spring.study.autowire.Person; public class Main { public static void main(String[] args) {
// TODO Auto-generated method stub ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-relation.xml");
Address address = (Address) ctx.getBean("address");
System.out.println(address); address = (Address) ctx.getBean("address2");
System.out.println(address); Person person = (Person) ctx.getBean("person");
System.out.println(person); } }

下面是输出结果

五月 20, 2017 4:23:24 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3eb07fd3: startup date [Sat May 20 16:23:24 CST 2017]; root of context hierarchy
五月 20, 2017 4:23:25 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-relation.xml]
Address [city=Beijing1, street=WuDaoKou]
Address [city=Beijing1, street=DaZhongSi]
Person [name=Tom, address=Address [city=Beijing1, street=WuDaoKou], car=null]

可以看到car是null,如果我必须要car不是null,也就是说person依赖于null,那么如何做?

如下做法:

<?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.xsd"> <bean id="address" class="logan.spring.study.autowire.Address"
p:city="Beijing1" p:street="WuDaoKou"></bean> <bean id="address2" p:street="DaZhongSi" parent="address"></bean> <bean id="person" class="logan.spring.study.autowire.Person"
p:name="Tom" p:address-ref="address" depends-on="car"></bean> </beans>

输出错误结果

五月 20, 2017 4:27:50 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3eb07fd3: startup date [Sat May 20 16:27:50 CST 2017]; root of context hierarchy
五月 20, 2017 4:27:50 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-relation.xml]
五月 20, 2017 4:27:50 下午 org.springframework.context.support.ClassPathXmlApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'car' available
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'car' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1207)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:296)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at logan.spring.study.relation.Main.main(Main.java:14)

如下配置就不会报错

<?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.xsd"> <bean id="address" class="logan.spring.study.autowire.Address"
p:city="Beijing1" p:street="WuDaoKou"></bean> <bean id="address2" p:street="DaZhongSi" parent="address"></bean> <bean id="car" class="logan.spring.study.autowire.Car"
p:brand="Audi" p:price="300000"></bean>
<bean id="person" class="logan.spring.study.autowire.Person"
p:name="Tom" p:address-ref="address" depends-on="car"></bean> </beans>

Spring入门第七课的更多相关文章

  1. Spring入门第六课

    XML配置里的Bean自动装配 Spring IOC容器可以自动装配Bean.需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式 ByType(根据类型自动装配):若I ...

  2. Spring入门第五课

    集合属性 在Spring中可以通过一组内置的xml标签(如:<list>,<set>,<map>)来配置集合属性. 配置java.util.List类型的属性,需要 ...

  3. Spring入门第四课

    注入参数详解:null值和级联属性 可以使用专用的<null/>元素标签为Bean的字符串或其他对象类型的属性注入null值. 和Struts,Hiberante等框架一样,Spring支 ...

  4. Spring入门第三课

    属性注入 属性注入就是通过setter方法注入Bean的属性值或依赖的对象. 属性植入使用<property>元素,使用name属性指定Bean的属性名称,value属性或者<val ...

  5. Spring入门第十三课

    通过FactoryBean来配置Bean package logan.spring.study.factoryBean; public class Car { private String brand ...

  6. Spring入门第十一课

    IOC容器中Bean的生命周期 Spring IOC容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的任务. Spring IOC容器对Bean的生命周期进行管理的过 ...

  7. Spring入门第十课

    Spring表达式语言:SpEL Spring表达式语言(简称SpEL)是一个支持运行时查询和操作对象图的强大的表达式语言. 语法类似于EL:SpEL使用#{...}作为定界符,所有在大括号中的字符都 ...

  8. Spring入门第八课

    看如下代码 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...

  9. Spring入门第十七课

    AOP编程 问题: 代码混乱: 越来越多的非业务需求(日志和验证等)加入后,原有的业务方法急剧膨胀,每个方法在处理核心逻辑的同事还必须兼顾其他多个关注点. 代码分散:以日志需求为例,只是为了满足这个单 ...

随机推荐

  1. EasyPlayerPro(Windows)流媒体播放器开发之跨语言调用

    下面我们来讲解一下关于EasyPlayerPro接口的调用,主要分为C++和C#两种语言,C++也可以基于VC和QT进行开发,C++以VC MFC框架为例进行讲解,C#以Winform框架为例进行讲解 ...

  2. SQLServer中游标实例介绍(转)

    引言 我们先不讲游标的什么概念,步骤及语法,先来看一个例子: 表一 OriginSalary                      表二 AddSalary 现在有2张表,一张是OriginSal ...

  3. 九度OJ 1065:输出梯形 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5379 解决:2939 题目描述: 输入一个高度h,输出一个高为h,上底边为h的梯形. 输入: 一个整数h(1<=h<=1000 ...

  4. Grunt学习笔记【5】---- expand使用方法

    本文主要讲expand使用方法. 当你希望处理大量的单个文件时,这里有一些附加的属性可以用来动态的构建一个文件列表.这些属性都可以用于 Compact 和 Files Array 文件映射格式. ex ...

  5. Delphi 完全时尚手册之 Visual Style 篇

    这里先说说两个概念:Theme(主题)和 Visual Style .Theme 最早出现在 Microsoft Plus! for Windows 95 中,是 Windows 中 Wallpape ...

  6. linux c编程:网络编程

    在网络上,通信服务都是采用C/S机制,也就是客户端/服务器机制.流程可以参考下图: 服务器端工作流程: 使用socket()函数创建服务器端通信套接口 使用bind()函数将创建的套接口与服务器地址绑 ...

  7. 【linux】crontab的环境变量问题

    今天遇到一个奇怪的问题,同样一个脚本,手动执行没问题,加入到crontab中,就出现无法运行的情况,第一反应是环境变量问题 环境说明: 操作系统:centos 用户:test用户通过sudo su切换 ...

  8. 程序连接Oracle数据库出现未找到提供程序.该程序可能未正确安装错误提示

    好不容易使用plsql可以成功连上数据库了,应用程序连接数据库却出现了问题 其实解决这个问题也简单: 1.  查看oracle安装目录下的BIN目录,E:\app\Administrator\prod ...

  9. CodeChef - ANDMIN —— 线段树 (结点最多被修改的次数)

    题目链接:https://vjudge.net/problem/CodeChef-ANDMIN Read problems statements in Mandarin Chinese, Russia ...

  10. Spring Boot2.0之纯手写框架

    框架部分重点在于实现原理,懂原理! 废话不多说,动手干起来! SpringMVC程序入口? 没有配置文件,Spring 容器是如何加载? 回顾我们之前搭建Spring Boot项目使用的pom 引入的 ...