Fork me on GitHub

spring之bean之间的关系

两种关系:继承、依赖

一、继承关系

Address.java

package com.gong.spring.beans.autowire;

public class Address {
    private String city;
    private String street;
    
    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    @Override
    public String toString() {
        return "Address [city=" + city + ", street=" + street + "]";
    }

    
}

beans-relation.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"
    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="com.gong.spring.beans.autowire.Address" 
    p:city="武汉" p:street="络南街道"></bean>
    <!-- 使用parent指定指定哪个bean的配置,子bean可以覆盖父bean的配置 -->
    <bean id="address2" class="com.gong.spring.beans.autowire.Address" parent="address" 
    p:street="珞狮街道"></bean>
</beans>

Main.java

package com.gong.spring.beans.autowire;

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

public class Main {
    public static void main(String[] args) {
        //1.创建spring的IOC容器对象
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-relation.xml");
        //2.从容器中获取Bean实例
        Address address = (Address) ctx.getBean("address"); 
        System.out.println(address.toString());
        Address address2 = (Address) ctx.getBean("address2"); 
        System.out.println(address2.toString());
    }
}

输出:

address2继承了address的city配置,因此city=武汉。

当然,我们也可以使用abstract来表明一个Bean是一个抽象bean。抽象bean可以作为一个模板,且不能被实例化。同时,如果一个bean没有声明class,那么该bean也是一个抽象bean,且必须指定abstract="true"。

<bean id="address" class="com.gong.spring.beans.autowire.Address" abstract="true"
    p:city="武汉" p:street="络南街道"></bean>

此时,在进行实例化就会报错

Address address = (Address) ctx.getBean("address"); 

将抽象bean作为父bean,可以实例化它的子bean:

    Address address2 = (Address) ctx.getBean("address2"); 
    System.out.println(address2.toString());

二、依赖关系

Car.java

package com.gong.spring.beans.autowire;

public class Car {
    
    public Car() {
    }

    public Car(String name) {
        this.name = name;
    }
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Car [name=" + name + "]";
    }
    
}

Student.java

package com.gong.spring.beans.autowire;
import java.util.List;
import java.util.Map;

public class Student {
    
    private String name;
    private int age;
    private double score;
    private Car car;
    private Address address;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public double getScore() {
        return score;
    }
    public void setScore(double score) {
        this.score = score;
    }
    public Car getCar() {
        return car;
    }
    public void setCar(Car car) {
        this.car = car;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", score=" + score + ", car=" + car + ", address=" + address
                + "]";
    }
    
    
}

beans-relation.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"
    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="car" class="com.gong.spring.beans.autowire.Car" p:name="baoma"></bean>
    <bean id="address" class="com.gong.spring.beans.autowire.Address" 
    p:city="武汉" p:street="络南街道"></bean>
    <!-- 要求配置Student时,要依赖于Car-->
    <bean id="student" class="com.gong.spring.beans.autowire.Student" p:name="tom"
    p:age="12" p:score="99.00" autowire="byName" depends-on="car"></bean>
</beans>

spring允许用户通过depends-on属性设定bean前置依赖bean,前置依赖bean会在本Bean实例化之前就创建好。如果前置依赖于多个Bean,则可以通过逗号,空格的方式来配置bean的名称。

Main.java

package com.gong.spring.beans.autowire;

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

public class Main {
    public static void main(String[] args) {
        //1.创建spring的IOC容器对象
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-relation.xml");
        //2.从容器中获取Bean实例
        Student student = (Student) ctx.getBean("student"); 
        System.out.println(student.toString());
    }
}

输出:

posted @ 2020-01-05 14:51  西西嘛呦  阅读(243)  评论(0编辑  收藏  举报