欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自动装配

程序员文章站 2022-04-19 22:40:30
...

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

byType(根据类型自动装配): 若 IOC 容器中有多个与目标 Bean 类型一致的 Bean. 在这种情况下, Spring 将无法判定哪个 Bean 最合适该属性, 所以不能执行自动装配.

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

缺点

在 Bean 配置文件里设置autowire 属性进行自动装配将会装配 Bean 的所有属性. 然而, 若只希望装配个别属性时,autowire 属性就不够灵活了.

autowire 属性要么根据类型自动装配, 要么根据名称自动装配, 不能两者兼而有之.

一般情况下,在实际的项目中很少使用自动装配功能,因为和自动装配功能所带来的好处比起来,明确清晰的配置文档更有说服力一些

Main

package com.spring.beans.autowire;

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

public class Main {

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("/com/spring/beans/autowire/beans-autowire.xml");
		Person p = (Person)ac.getBean("person");
		System.out.println(p);
	}
}
/*
Car's constructor
Person [name=Tom, address=null, car=Car [brand=Audi, price=222333.0]]
*/

/com/spring/beans/autowire/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: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="address1" class="com.spring.beans.autowire.Address"
    p:city="Beijing" p:street="Hilongguan"></bean>
    
    <bean id="car" class="com.spring.beans.autowire.Car" 
    p:brand="Audi" p:price="222333"></bean>
    <!-- 
    	可以使用autowire属性指定自动装配的方式,
    	byName根据bean的名字和当前bean的setter风格的属性名进行自动装配 ,若有匹配则进行自动装配,没有匹配,则不装配
    	byType根据bean的类型和当前bean的属性的类型进行自动装配,若IOC容器中有一个以上的类型匹配的bean,则抛异常
    -->
    <bean id="person" class="com.spring.beans.autowire.Person"
    p:name="Tom" autowire="byName"></bean>

</beans>
Person
package com.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

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

Car

package com.spring.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 constructor");
	}
	
}