Spring_自动装配 & bean之间的关系 & bean的作用域


1.自动装配

beans-autowire.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:util="http://www.springframework.org/schema/util"
    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
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

<bean id="address2" class="com.aff.spring.beans.autowire.Address" p:city="hefei"  p:street="wanda">bean>
<bean id="address" class="com.aff.spring.beans.autowire.Address" p:city="wuhu"  p:street="wanda">bean>
<bean id="car" class="com.aff.spring.beans.autowire.Car" p:brand="audi" p:price="250000">bean>

<bean id="person" class="com.aff.spring.beans.autowire.Person"   p:name="Hxl" autowire="byName">bean>
beans>

Main.java

package com.aff.spring.beans.autowire;

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

public class Main {
public static void main(String[] args) {
     ApplicationContext  ctx = new ClassPathXmlApplicationContext("beans-autowire.xml");
     Person person = (Person) ctx.getBean("person");
     System.out.println(person);
     //Person [name=Hxl, addres=Address [city=hefei, street=wanda], car=Car [brand=audi, price=250000.0]]

}
}

Person.java

package com.aff.spring.beans.autowire;

public class Person {
    private String name;
    private Address address;
    private Car car;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    public Car getCar() {
        return car;
    }
    public void setCar(Car car) {
        this.car = car;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", address=" + address + ", car=" + car + "]";
    }

}

Address.java

package com.aff.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 + "]";
    }
    public Address() {
        super();
    }
    public Address(String city, String street) {
        super();
        this.city = city;
        this.street = street;
    }
}

Car.java

package com.aff.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 + "]";
    }
    public Address() {
        super();
    }
    public Address(String city, String street) {
        super();
        this.city = city;
        this.street = street;
    }
}

2.bean之间的关系:继承;依赖

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" p:city="hefei" p:street="wanda" abstract="true">bean>
    
    
    <bean id="address2" class="com.aff.spring.beans.autowire.Address"  p:street="buxinjie" parent="address">bean>
    
    <bean id="car" class="com.aff.spring.beans.autowire.Car" p:brand="audi" p:price="260000">bean>
    
     
    <bean id="person" class="com.aff.spring.beans.autowire.Person" p:name="Tom" p:address-ref="address2" depends-on="car">bean>
beans>

Main

package com.aff.spring.beans.relation;

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

import com.aff.spring.beans.autowire.Address;
import com.aff.spring.beans.autowire.Person;

public class Main {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-relation.xml");
        // Address address = (Address) ctx.getBean("address");
        // System.out.println(address);
        // Address [city=hefei, street=wanda]

        Address address2 = (Address) ctx.getBean("address2");
        System.out.println(address2);
        // Address [city=hefei, street=buxinjie]

        Person person = (Person) ctx.getBean("person");
        System.out.println(person);
        //Person [name=Tom, address=Address [city=hefei, street=buxinjie], car=null]

    }
}

3.bean 的作用域:singleton;prototype;

beans-scope.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.aff.spring.beans.autowire.Car" scope="singleton">
    <property name="brand" value="audi">property>
    <property name="price" value="250000">property>
    bean>
    
beans>

Main

package com.aff.spring.beans.scope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.aff.spring.beans.autowire.Car;

public class Main {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");
        
        Car car = (Car) ctx.getBean("car");
        Car car2 = (Car) ctx.getBean("car");
        System.out.println(car==car2);
        //true
    }
}

 目录