spring 配置bean-自动装配

概要:(蓝色为本节所讲)

spring 配置bean-自动装配



XML配置里的Bean自动装配

  • Spring IOC容器可以自动装配Bean。需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式
  • byType(根据类型自动装配):若IOC容器中有多个与目标Bean类型一致的Bean,这种情况下,Spring将无法判断哪个Bean最合适该属性,所以不能执行自动装配
  • byName(根据名称自动装配):必须将目标Bean的名称和属性名设置的完全相同
  • constructor(通过构造器自动装配):当Bean中存在多个构造器时,此种自动装配方式将会很复杂,不推荐使用

XML配置里的Bean自动装配的缺点
  • 在Bean配置文件里设置autowire属性进行自动装配将会配置Bean的所有引用属性。然而,若只希望装配个别属性时,autowire属性就不够灵活了
  • autowire属性要么根据类型自动装配,要么根据名称自动装配,不能两者兼而有之
  • 一般情况下,在实际的项目中很少使用自动装配功能,因为和自动装配功能所带来的好处比起来,明确清晰的配置文档更有说服力一些


代码实例详解:

目录结构:(只用到com.coslay.beans.autowire)

spring 配置bean-自动装配


Address.java

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

}


Person.java
package com.coslay.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.coslay.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);
	}
}


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



你可能感兴趣的:(spring)