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

Posted cklovefan

tags:

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

Spring IOC 容器可以自动装配Bean,需要做的是在<bean>的autowire属性里指定自动装配的模式

1)byType 根据类型自动装配,若IOC 容器中有多个目标Bean类型一致的Bean,Spring将无法判定哪个Bean最适合该属性,不能执行自动装配

2)byName 根据名称自动装配,必须将目标Bean名称和属性名设置的完全相同

配置方法:

Address.java

package com.spring.autowire;

public class Address {
    private String city;
    private String location;
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getLocation() {
        return location;
    }
    public void setLocation(String location) {
        this.location = location;
    }
    @Override
    public String toString() {
        return "Address [city=" + city + ", location=" + location + "]";
    }
}

2、Car.java

package com.spring.autowire;

public class Car {
    private String brand;
    private String price;
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "Car [brand=" + brand + ", price=" + price  + "]";
    }
}

3、Person.java

package com.spring.autowire;

public class Person {
    private String name;
    private String age;
    private Address address;
    
    @Override
    public String toString() {
        return "Person [address=" + address + ", age=" + age + ", car=" + car
                + ", name=" + name + "]";
    }
    private Car car;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    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;
    }
}

4、配置文件bean-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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="car" class="com.spring.autowire.Car">
        <property name="brand" value="Audi"></property>
        <property name="price" value="300000"></property>
    </bean>
    
    <bean id="address" class="com.spring.autowire.Address">
        <property name="city" value="beijing"></property>
        <property name="location" value="fangshan"></property>
    </bean>
    
    <bean id="person" class="com.spring.autowire.Person" autowire="byName">
    </bean>
</beans>

5、测试

package com.spring.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("bean-autowire.xml");
        Person person = (Person)ctx.getBean("person");
        System.out.println(person);
    }
}

  可以使用autowire 属性指定自动装配的方式,byName 根据bean 的名字和当前bean的setter风格的属性名进行自动装配 

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

Spring学习笔记之装配Bean

Spring4.0学习笔记006——Bean的配置(基于注解)

Spring4.0学习笔记006——Bean的配置(基于注解)

Spring4.0学习笔记002——Spring应用初识

Spring学习笔记——装配Bean

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