自动装配

/ Spring / 没有评论 / 243浏览

我们知道spring框架的IOC容器主要的功能就是为我们实例化对象,我们也可以称之为装配对象。IOC容器装配对象的方式很简单,只要我们把要装配的对象添加到spring配置文件中并通过bean标签指定对象的全路径,然后就可以通过ApplicationContext接口或BeanFactory接口完成对象的装配。上述装配对象的方式虽然比较简单,但是,如果我们要装配很多个对象时,上述方式就显的不是那么友好了。因为我们要将需要装配的所有对象都添加到spring配置文件中。当然我们也可以这样做,但是这种必须显式配置的方式相当于重复工作,显然不是最好的解决办法。spring框架为了解决上述装配的弊端,于是提供了一种比较方便便捷的装配方式。就是可以按照某种规则进行对象的自动装配,这也就是我们本篇的重点。下面我们看一下spring框架中都支持哪种类型的自动装配。

当我们在使用bean标签装配对象时,会有一个autowire属性,就是这个属性可以让我们配置当前对象的自动装配类型的。对象的自动装配类型分为四种,它们分别是:

根据名字进行自动匹配,也就是如果对象中有一个属性名字和IOC容器中的一个对象的名字相同,那么就会自动将这个对象注入到当前对象的属性中。我们可以用下面的方式验证。

public class Work {
private String address;

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

@Override
public String toString() {
return "Work{" +
"address='" + address + '\'' +
'}';
}
}
public class User {

private Work work;

public Work getWork() {
return work;
}

public void setWork(Work work) {
this.work = work;
}

@Override
public String toString() {
return "User{" +
"work=" + work +
'}';
}
}
<bean id="work" class="com.jilinwula.spring.Work" p:address="吉林乌拉"/>
<bean id="user" class="com.jilinwula.spring.User" p:work-ref="work"/>
@Test
public void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
User user = applicationContext.getBean("user", User.class);
System.out.println(user);
}
User{work=Work{address='吉林乌拉'}}
<bean name="work" class="com.jilinwula.spring.Work" p:address="吉林乌拉"/>
<bean autowire="byName" id="user" class="com.jilinwula.spring.User"/>
User{work=Work{address='吉林乌拉'}}

如果我们将Word装配的名字修改了, 那么就不会执行自动装配了,因为在IOC容器中没有找到和word属性相匹配的名字。

<bean name="work2" class="com.jilinwula.spring.Work" p:address="吉林乌拉"/>
<bean autowire="byName" id="user" class="com.jilinwula.spring.User"/>
User{work=null}

根据类型进行自动装配,也就是如果对象中有一个属性是对象类型的,正好IOC容器中也有这个对象,那么就会自动将这个对象注入到当前对象的属性中。

<bean class="com.jilinwula.spring.Work" p:address="吉林乌拉"/>
<bean autowire="byType" id="user" class="com.jilinwula.spring.User"/>
User{work=Work{address='吉林乌拉'}}

和byType按类型装配的方式类似,只是这种装配的类型,针对的只是构造方法,而不是属性。

public class User {

private Work work;

public User(Work work) {
this.work = work;
}

public Work getWork() {
return work;
}

public void setWork(Work work) {
this.work = work;
}

@Override
public String toString() {
return "User{" +
"work=" + work +
'}';
}
}
<bean class="com.jilinwula.spring.Work" p:address="吉林乌拉"/>
<bean autowire="constructor" id="user" class="com.jilinwula.spring.User"/>
User{work=Work{address='吉林乌拉'}}

如果我们把User对象中的带Work类型参数的构造方法去掉,则上述装配不会成功。

public class Work {
private String address;

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

@Override
public String toString() {
return "Work{" +
"address='" + address + '\'' +
'}';
}
}
User{work=null}

default装配类型并不代表不进行自动装配,而是根据beans标签中设置的default-autowire属性的类型来进行自动装配的。

public class User {

private Work work;

public User() {
}

public User(Work work) {
this.work = work;
}

public Work getWork() {
return work;
}

public void setWork(Work work) {
this.work = work;
}

@Override
public String toString() {
return "User{" +
"work=" + work +
'}';
}
}
<?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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd" default-autowire="byName">

<bean class="com.jilinwula.spring.Work" p:address="吉林乌拉"/>
<bean autowire="default" id="user" class="com.jilinwula.spring.User"/>

</beans>
User{work=null}
<?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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd" default-autowire="byType">

<bean class="com.jilinwula.spring.Work" p:address="吉林乌拉"/>
<bean autowire="default" id="user" class="com.jilinwula.spring.User"/>

</beans>
User{work=Work{address='吉林乌拉'}}
<?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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd" default-autowire="constructor">

<bean class="com.jilinwula.spring.Work" p:address="吉林乌拉"/>
<bean autowire="default" id="user" class="com.jilinwula.spring.User"/>

</beans>
User{work=Work{address='吉林乌拉'}}

如果我们不想采用自动装配功能,可以将autowire属性设置为no。如果我们不配置autowire属性,则对象的默认装配类型为default。

<?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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd" default-autowire="constructor">

<bean class="com.jilinwula.spring.Work" p:address="吉林乌拉"/>
<bean autowire="no" id="user" class="com.jilinwula.spring.User"/>

</beans>
User{work=null}
<?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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd" default-autowire="constructor">

<bean class="com.jilinwula.spring.Work" p:address="吉林乌拉"/>
<bean id="user" class="com.jilinwula.spring.User"/>

</beans>
User{work=Work{address='吉林乌拉'}}
<?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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd" default-autowire="no">

<bean class="com.jilinwula.spring.Work" p:address="吉林乌拉"/>
<bean id="user" class="com.jilinwula.spring.User"/>

</beans>
User{work=null}