Spring框架学习笔记——自动装配

Posted 灼眼的健

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring框架学习笔记——自动装配相关的知识,希望对你有一定的参考价值。

1、通过bean标签的autowire属性可以实现bean属性的自动装配。

  创建一个新的Spring配置文件beans-autowire.xml,这里我们配置了3个bean,Address、Car、People,其中People自动装配了另外两个bean,使用的是bean标签的autowire属性,这个例子里如果将car改为car2打印出来的Person bean 的Car属性为null,即byName根据bean的setter风格进行自动装配有就装没有就不装。而使用byType的话如果配置有两个类型相同的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"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

    <bean id="address" class="com.broadtext.beans.autowire.Address">
        <property name="city" value="Hefei"></property>
        <property name="street" value="Feixilu"></property>
    </bean>
    
    <bean id="car" class="com.broadtext.beans.autowire.Car">
        <property name="brand" value="Audi"></property>
        <property name="price" value="300000"></property>
    </bean>
    
    <!-- 可以使用autowire属性指定自动装配的方式,
    byName根据bean的setter风格进行自动装配有就装没有就不装,
    byType根据bean的类型和bean的属性的类型进行自动装配,一个以上类型匹配则抛异常
     -->
    <bean id="person" class="com.broadtext.beans.autowire.Person" autowire="byName" p:name="hj"></bean>
  
</beans>

2、Address.java、Car.java、People.java、Main.java

Address.java

package com.broadtext.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 + "]";
    }
}

Car.java

package com.broadtext.beans.autowire;

public class Car {
    private String brand;
    private double price;

    public String getBrand() {
        return brand;
    }

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

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

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

    public Car() {
        System.out.println("car`s construstor");
    }

}

People.java

package com.broadtext.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 + "]";
    }
}

Main.java

package com.broadtext.beans.autowire;

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

public class Main {

    /**
     * <p>Description: 测试bean的自动装配</p>
     */
    public static void main(String[] args) {

        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-autowire.xml");
        Person person = (Person) ctx.getBean("person");
        System.out.println(person);
    }

}

以上是关于Spring框架学习笔记——自动装配的主要内容,如果未能解决你的问题,请参考以下文章

Spring 框架学习---- bean自动装配注解开发

Spring4.0学习笔记 —— 自动装配

Spring学习笔记——装配Bean

《spring实战》学习笔记-第二章:装配bean

Spring框架学习:Spring @Autowired实现自动装配

spring框架学习:Bean的装配方式 ——基于注解的装配自动装配