The relationship between the spring of bean

Two kinds of relationships: inheritance, dependent

First, inheritance

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 the above mentioned id = "address" class = "com.gong.spring.beans.autowire.Address"  
    the p-: City = "Wuhan" p: street = "network South Street"> </ bean>
     ! <- use the specified parent specify which configuration bean, bean can override the parent sub bean configuration -> 
    <bean ID = "address2" class = "com.gong.spring.beans.autowire.Address" parent = "address"  
    P: Street = "Luoshi street "> </ bean>
</beans>

Main.java

package com.gong.spring.beans.autowire;

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

public  class the Main {
     public  static  void main (String [] args) {
         // 1. Create spring IOC container object of 
        the ApplicationContext CTX = new new the ClassPathXmlApplicationContext ( "Beans-relation.xml" );
         // 2. Bean instance acquired from the container 
        address = the address (the address) ctx.getBean ( "address" );
        System.out.println(address.toString());
        Address address2 = (Address) ctx.getBean("address2"); 
        System.out.println(address2.toString());
    }
}

Output:

address2 address configuration inherited the city, so city = Wuhan.

Of course, we can also use the abstract to indicate a Bean is an abstract bean. Abstract bean can be used as a template, and can not be instantiated. Meanwhile, if the bean does not declare a class, then the bean is a bean abstract, and must specify abstract = "true".

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

In this case, the error will be performing the instantiation

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

Abstract bean bean as a parent, it can be instantiated sub bean:

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

Second, dependence

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 allows the user to pre-set the bean by bean dependence depends-on property, the front will depend on the bean created before this Bean instantiated. If pre dependent on multiple Bean, you can configure the name of the bean through commas, spaces.

Main.java

package com.gong.spring.beans.autowire;

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

public  class the Main {
     public  static  void main (String [] args) {
         // 1. Create spring IOC container object of 
        the ApplicationContext CTX = new new the ClassPathXmlApplicationContext ( "Beans-relation.xml" );
         // 2. Bean instance acquired from the container 
        Student = Student (Student) ctx.getBean ( "Student" );
        System.out.println(student.toString());
    }
}

Output:

Guess you like

Origin www.cnblogs.com/xiximayou/p/12152453.html