自动装配
程序员文章站
2022-04-19 22:34:24
...
XML 配置里的 Bean 自动装配
Spring IOC 容器可以自动装配 Bean. 需要做的仅仅是在 <bean> 的 autowire 属性里指定自动装配的模式
byType(根据类型自动装配): 若 IOC 容器中有多个与目标 Bean 类型一致的 Bean. 在这种情况下, Spring 将无法判定哪个 Bean 最合适该属性, 所以不能执行自动装配.
byName(根据名称自动装配): 必须将目标 Bean 的名称和属性名设置的完全相同.
constructor(通过构造器自动装配): 当 Bean 中存在多个构造器时, 此种自动装配方式将会很复杂. 不推荐使用
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="address1" class="com.learn.spring.autowire.Address">
<property name="city" value="BeiJing" ></property>
<property name="street" value="HuiLongGuan"></property>
</bean>
<bean id="address2" class="com.learn.spring.autowire.Address">
<property name="city" value="ShangHai" ></property>
<property name="street" value="PuDong"></property>
</bean>
<bean id="car" class="com.learn.spring.autowire.Car" p:brand="Audi" p:price="400000">
</bean>
<!--
autowire:
byName: 通过bean的id值与要进行注入的属性名进行匹配
byType: 通过bean的class值与要进行注入的属性的类型进行匹配.
-->
<bean id="person" class="com.learn.spring.autowire.Person" autowire="byType">
<property name="name" value="Tom"></property>
</bean>
</beans>
package com.learn.spring.autowire;
public class Address {
private String city ;
private String street ;
public Address() {
// TODO Auto-generated constructor stub
System.out.println("Address 's Constructor ......");
}
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.learn.spring.autowire;
public class Car {
private String brand ;
private double price ;
public Car() {
// TODO Auto-generated constructor stub
}
public Car(String brand, double price) {
super();
this.brand = brand;
this.price = 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.learn.spring.autowire;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
//1.实例化容器
ApplicationContext ctx =
new ClassPathXmlApplicationContext("spring-autowire.xml");
//2.从容器中获取bean
Person person = (Person)ctx.getBean("person");
System.out.println(person);
}
}
package com.learn.spring.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
+ "]";
}
}