XML 配置里的 Bean 自动装配
- 在 Bean 配置文件里设置 autowire 属性进行自动装配将会装配 Bean 的所有属性. 然而, 若只希望装配个别属性时, autowire 属性就不够灵活了.
- autowire 属性要么根据类型自动装配, 要么根据名称自动装配, 不能两者兼而有之.
- 一般情况下,在实际的项目中很少使用自动装配功能,因为和自动装配功能所带来的好处比起来,明确清晰的配置文档更有说服力一些
<?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.atguigu.spring.beans.autowire.Address" p:city="北京" p:street="朝阳街"></bean>
<bean id="car" class="com.atguigu.spring.beans.autowire.Car" p:brand="吉利" p:price="20000"></bean>
<!--<bean id="person" class="com.atguigu.spring.beans.autowire.Person" p:name="小明" p:address-ref="address" p:car-ref="car"></bean> -->
<!-- 可以使用autowire属性自动装配,byName根据setter名字和bean的名字进行匹配装配
byType根据bean的类型和当前bean的属性的类型进行自动装配
-->
<bean id="person" class="com.atguigu.spring.beans.autowire.Person" p:name="小明" autowire="byName"></bean>
</beans>
package com.atguigu.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 + "]";
}
}
package com.atguigu.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 + "]";
}
}
package com.atguigu.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 + "]";
}
}
package com.atguigu.spring.beans.autowire;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext apc = new ClassPathXmlApplicationContext("beans-autowire.xml");
Person person = (Person) apc.getBean(Person.class);
System.out.println(person);
}
}
结果:
六月 08, 2018 10:35:29 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6193b845: startup date [Fri Jun 08 10:35:29 CST 2018]; root of context hierarchy
六月 08, 2018 10:35:29 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-autowire.xml]
Person [name=小明, address=Address [city=北京, street=朝阳街], car=Car [brand=吉利, price=20000.0]]