一. 储备知识
本文章项目源码已上传到本博客的“资源”处,可自行前往免费下载
1.1 自动装配的概念
- 手动装配:以value或ref明确指定属性值
- 自动装配:根据指定的装配规则,不需要明确指定,Spring自动将匹配的属性值注入bean中,在
<bean>
标签中使用autowire属性指定装配模式
1.2 装配模式
- 根据类型自动装配(byType):将类型匹配的bean注入到另一个bean中。若IOC容器中有多个与目标bean类型一致的bean,Spring将无法判定哪个bean最适合该属性,所以不能执行自动装配。
- 根据名称自动装配(byName):必须将目标bean的名称和属性名设置的完全相同。
1.3 选用建议
相对于使用注解的方式自动装配,在XML文件中进行自动装配略显笨拙,在项目中更多的是使用注解的方式实现。
二. 例子
使用spring的自动装配并输出信息
Address.java
package com.atguigu.spring.autowire;
public class Address {
private String province;
private String city;
public Address() {
super();
// TODO Auto-generated constructor stub
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Address [province=" + province + ", city=" + city + "]";
}
}
Car.java
package com.atguigu.spring.autowire;
public class Car {
private String brand;
private Double price;
public Car() {
super();
// TODO Auto-generated constructor stub
}
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.atguigu.spring.autowire;
public class Person {
private String name;
private Address address;
private Car car;
public Person() {
super();
// TODO Auto-generated constructor stub
}
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 + "]";
}
}
spring-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.atguigu.spring.autowire.Address">
<property name="province" value="广东省"></property>
<property name="city" value="广州市"></property>
</bean>
<bean id="car" class="com.atguigu.spring.autowire.Car">
<property name="brand" value="宝马"></property>
<property name="price" value="10000"></property>
</bean>
<!-- 通过名字自动装配 -->
<!-- <bean id="person" class="com.atguigu.spring.autowire.Person" autowire="byName">
<property name="name" value="老马"></property>
</bean> -->
<!-- 通过类型自动装配 -->
<bean id="person" class="com.atguigu.spring.autowire.Person" autowire="byType">
<property name="name" value="老马"></property>
</bean>
</beans>
Main.java
package com.atguigu.spring.autowire;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context =
new ClassPathXmlApplicationContext("spring-autowire.xml");
Person person = context.getBean("person", Person.class);
System.out.println(person);
}
}