springIOC容器可以自动装配Bean,需要做的仅仅是在<bean>中的autowire属性里指定自动装配的方式。
ByType:根据类型自动装配。若IOC容器中有多个目标Bean类型一致的bean,在这种情况下,spring将无法判定那个bean最适合该属性,所以不能执行自动装配。
ByName:根据名称自动装配,必须将目标Bean的名称和属性名设置完全相同。
contructor:通过构造器自动装配,当Bean中存在多个构造器时,此种自动装配方式将会很复杂,不推荐使用,
Car.java
package com.gong.spring.beans.autowire; public class Car { public Car() { } public Car(String name) { this.name = name; } private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Car [name=" + name + "]"; } }
Address.java
package com.gong.spring.beans.autowire; public class Address { private String street; public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } @Override public String toString() { return "Address [street=" + street + "]"; } }
Student.java
package com.gong.spring.beans.autowire; import java.util.List; import java.util.Map; public class Student { private String name; private int age; private double score; private Car car; private Address address; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", score=" + score + ", car=" + car + ", address=" + address + "]"; } }
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="car" class="com.gong.spring.beans.autowire.Car" p:name="baoma"></bean> <bean id="address" class="com.gong.spring.beans.autowire.Address" p:street="络南街道"></bean> <bean id="address2" class="com.gong.spring.beans.autowire.Address" p:street="珞狮街道"></bean> <!-- <bean id="student" class="com.gong.spring.beans.autowire.Student" p:name="tom" p:age="12" p:score="99.00" p:address-ref="address" p:car-ref="car"></bean> --> <!-- 使用byName时,要装配的bean的名字要和类文件中的setter方法中的名字保持一致,若没有匹配的,则不装配 <bean id="student" class="com.gong.spring.beans.autowire.Student" p:name="tom" p:age="12" p:score="99.00" autowire="byName"></bean> --> <!-- byType根据bean的类型和当前Bean的属性自动装配,若IOC容器中有一个以上的bean可以匹配,则会抛出异常 --> <bean id="student" class="com.gong.spring.beans.autowire.Student" p:name="tom" p:age="12" p:score="99.00" autowire="byName"></bean> </beans>
Main.java
package com.gong.spring.beans.autowire; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { //1.创建spring的IOC容器对象 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-autowire.xml"); //2.从容器中获取Bean实例 Student student = (Student) ctx.getBean("student"); System.out.println(student.toString()); } }
XML配置里自动装配的缺点:
(1)自动装配将会装配Bean的所有属性,然而只需要装配个别属性时,自动装配就不够灵活;
(2)要么根据类型,要么根据名称自动装配,不能两者兼顾;
(3)一般而言,在实际项目中很少使用自动装配,因为与自动装配功能带来的好处相比,明确清晰的配置方式更有说服力;