Spring(三)——Bean的 继承关系、依赖关系、作用域

1、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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="ren1" class="test.Person">
        <property name="name" value="Hello World"></property>
        <property name="age" value="2333"></property>
    </bean>

    <bean id="ren2" class="test.Person" parent="ren1">
        <!-- <property name="name" value="Hello Spring"></property>
        <property name="age" value="666"></property> -->
    </bean>

</beans>
package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
        // 导入IOC容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

        // 获取Person对象
        Person person1 = (Person) ctx.getBean("ren1");
        Person person2 = (Person) ctx.getBean("ren2");

        // 输出Person
        System.out.println(person1);
        System.out.println(person2);
    }

}

ren2 的 bean 中是有一个 parent 的属性的,这个属性指向 ren1 bean,这说明 ren2 继承了 ren1 的内容的。

但是如果在ren2有属性值,那么将重写该属性值,类似于面向对象的重写。


Spring允许继承 bean 的配置,被继承的 bean 成为父 bean,继承这个父 bean 的bean 成为子 bean(这并非我们所说的父类和子类)。子 bean 可以覆盖父 bean 的属性。

这个继承有什么用呢?首先可以省去重复的配置,你只需要继承一个Bean就可以不需要配置那么多的属性了。父bean作为模板,添加abstract属性为true就行了,例如

<bean id="ren1" class="test.Person" abstract="true">
        <property name="name" value="Hello World"></property>
        <property name="age" value="2333"></property>
</bean>

这样的话就不能创建ren1的实例,只用来被继承。

如果一个bean没有被指定class属性,那么它必须是抽象bean,即 abstract=true。

2、Bean的依赖关系

Car.java

package test;

public class Car {

    private String brand;
    private String carID;

    public Car() {
        super();
    }

}

Person.java

package test;

public class Person {

    private String name;
    private int age;
    private Car car;

    public Person() {

    }

}

applicationContext.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 id="che" class="test.Car">
        <property name="brand" value="Audi"></property>
        <property name="carID" value="123"></property>
    </bean>

    <bean id="ren" class="test.Person">
        <property name="name" value="Hello Spring"></property>
        <property name="age" value="666"></property>
        <property name="car" ref="che"></property>
    </bean>

</beans>

在这里,我们模拟的是每个人有一辆车,而车又是一个JavaBean,因此我们这里需要用到引用,即在property中不再是value属性了而是ref

这里需要注意的是,如果没有定义car的JavaBean那么就会报错的。所以我们才说Person依赖于Car。这也很容易理解,我都没写id,怎么填写ref的值呢?

** 小结 **

  1. 继承关系,在子bean中添加parent属性,属性值为父bean的id。同时父bean可以添加abstract属性作为子bean的模版,变成抽象bean。

  2. 依赖关系。首先需要定义依赖的bean(此例为Car),然后在被依赖bean(此例为Person)中的属性值用ref取代value,并指向依赖bean的ID。

3、Bean的作用域

默认情况下,IOC容器,即那个xml文件只会为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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="che" class="test.Car">
        <property name="brand" value="Audi"></property>
        <property name="carID" value="123"></property>
    </bean>

</beans>
package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
        // 导入IOC容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

        // 获取Person对象
        Car car1 = (Car) ctx.getBean("che");
        Car car2 = (Car) ctx.getBean("che");

        // 输出Person
        System.out.println(car1 == car2);
    }

}

这里的输出结果是 true 说明这两个是同一个对象的。


修改配置文件:

<bean id="che" class="test.Car" scope="prototype">
  <property name="brand" value="Audi"></property>
  <property name="carID" value="123"></property>
</bean>

运行Mian.java可以看到输出结果为false

我们看到scope的属性,这里singleton当然就是这个属性的默认值了,将 scope="prototype" 即原型。

原型是指:每次向容器中获取bean对象,都会返回一个新的bean,即这两个bean不是同一个对象。

我们同时发现这个 scope 的取值还有 session 和 request ,这两个比较少用就不说了。

小结

  1. 使用bean的 scope 属性来配置bean的作用域。

  2. singleton:默认值。容器初始时创建bean实例,在这整个容器的声明周期内只创建这一个bean

  3. prototype:原型的。容器初始化不创建bean的实例。而在每次请求时都创建一个新的Bean实例,并返回。

  4. session和request:少用。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,519评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,842评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,544评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,742评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,646评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,027评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,513评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,169评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,324评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,268评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,299评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,996评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,591评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,667评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,911评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,288评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,871评论 2 341

推荐阅读更多精彩内容