IOC自动装载有两种形式

<?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">

     <!-- ByName -->
    <bean id="person" class="com.sunjian.entity.Person" autowire="byName">
        <property name="id" value="1"></property>
        <property name="name" value="张三疯"></property>
    </bean>
    <bean id="car" class="com.sunjian.entity.Car">
        <property name="id" value="1"></property>
        <property name="brand" value="野马"></property>
    </bean>

</beans>
<?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">

    <!-- ByType -->
    <bean id="person" class="com.sunjian.entity.Person" autowire="byType">
        <property name="id" value="1"></property>
        <property name="name" value="欧阳锋"></property>
    </bean>
    <bean id="car2" class="com.sunjian.entity.Car">
        <property name="id" value="2"></property>
        <property name="brand" value="法拉利"></property>
    </bean>

</beans>

class

package com.sunjian.entity;

public class Car {
    private Integer id;
    private String brand;

    public void setId(Integer id) {
        this.id = id;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public Car(){
        System.out.println("创建了Car对象");
    }

    public Car(Integer id, String brand) {
        this.id = id;
        this.brand = brand;
    }

    @Override
    public String toString() {
        return "Car{" +
                "id=" + id +
                ", brand='" + brand + '\'' +
                '}';
    }
}
package com.sunjian.entity;

public class Person {
    private Integer id;
    private String name;
    private Car car;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", car=" + car +
                '}';
    }
}

test class

package com.sunjian.test;

import com.sunjian.entity.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author sunjian
 * @date 2020/3/14 15:17
 */
public class Test3 {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("autowired.xml");
        Person person = (Person) applicationContext.getBean("person");
        System.out.println(person);

        applicationContext = new ClassPathXmlApplicationContext("autowired2.xml");
        Person person2 = (Person) applicationContext.getBean("person");
        System.out.println(person2);
    }
}
创建了Car对象
Person{id=1, name='张三疯', car=Car{id=1, brand='野马'}}
创建了Car对象
Person{id=1, name='欧阳锋', car=Car{id=2, brand='法拉利'}}

OK.

05-12 06:51