白话Spring(基础篇)---自动装配

来源:互联网 发布:java图书管理系统 编辑:程序博客网 时间:2024/05/18 23:12

[一知半解,就是给自己挖坑]

在上一篇文章中,我们所有演示的用例,都是手动配置后才能使用的。那么是不是我们对每一个对象都需要我们亲自去配置呢?强大的Spring框架会让我们如此费力么?本文我们将来介绍一下Spring中如何使用自动装配来提供我们的bean

a.操作系统:win7 x64

b.开发工具:eclipse mars j2ee版本,maven3.3.2,Spring 4,junit4.12

c.复制Spring04工程,重命名为Spring05工程。具体工程结构图如下:


--------------------------------------------------------------------------------------------------------------------------------------------------------

第一种:autowire = no:这是默认的模式,如我们前面的用例中使用ref来讲Record装配到Customer中。

--------------------------

第二种:autowire = byName:表示按照名称来装配,即bean的id值需要与Customer中的属性值保持一致。

1.我们修改beans.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="Customer1" class="com.java.ingo.entity.Customer" autowire="byName"><property name="name" value="Tom"></property><property name="sex" value="male"></property><property name="age" value="22"></property></bean><bean id="record" class="com.java.ingo.entity.Record"><property name="company" value="ABCD"></property><property name="position" value="Engineer"></property><property name="address" value="Shanghai"></property></bean></beans>
2.在Test.java中,修改为下面的单元测试方法:
@org.junit.Testpublic void test1() {System.out.println("Start Test()");Customer customer = (Customer)ac.getBean("Customer1");System.out.println(customer);}
3.测试方法:运行test1方法,观察控制台输出即可。

注意:

a.这里的autowire = byName是添加在Customer的bean之上,即外部的bean对象之上。

b.bean的id值record必须完全相等于Customer对象中的属性值。

--------------------------------

第三种:autowire = byType:表示按照类型来装配,即bean的类型需要与Customer的属性值的id值保持一致

1.将上面的autowire = byName修改为autowire = byType。

2.测试方法:运行test1方法,观察控制台输出即可。

注意:

这里按照类型来注入时,bean的配置中不能出现,两个类型相同的bean。即这里不能出现两个record,即使这里id值不相同。

---------------------------------

第四种:autowire="constructor":表示按照构造函数来装配,即这里的record的对象类型与Customer中的构造函数的参数类型是一致的。

1.将上面的autowire = byType修改为autowire="constructor"。

2.在Customer.java中,增加一个构造函数,具体内容如下:

public Customer(Record record) {super();this.record = record;}
3.测试方法:运行test1方法,观察控制台输出即可。

----------------------------------

第五种:autowire="default"在每个bean中都一个autowire=default的默认配置它的含义是:采用beans和跟标签中的default-autowire="属性值"一样的设置。

1.修改beans.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"       <span style="white-space:pre">default-autowire="byName"</span>><bean id="Customer1" class="com.java.ingo.entity.Customer"  autowire="default"><property name="name" value="Tom"></property><property name="sex" value="male"></property><property name="age" value="22"></property></bean><bean id="record" class="com.java.ingo.entity.Record"><property name="company" value="ABCD"></property><property name="position" value="Engineer"></property><property name="address" value="Beijing"></property></bean></beans>
2.测试方法:运行test1方法,观察控制台输出即可。

注意:这里在beans标签中我们增加了default-autowire="byName"。

--------------------------------------------------------------------------------------------------------------------------------------------------------

至此,白话Spring(基础篇)---自动装配结束

特别提醒:

1.如果我们在beans中配置了default-autowire="类型"的话,那么这个配置将是在全局作用域内有效的。

2.对于本节内容,我们建议慎用Spring的自动装配机制。

原因是:

1.按照名称自动注入时,属性命名与id命名必须规范。

2.按照类型注入式又必须保证beans内部只能有一个类型符合要求的bean。

3.其他限制请参考上文。

4.最重要的,最重要的,最重要的,自动注入的方式屏蔽了装配的细节,对于初学者容易产生潜在的错误。

参考资料:

Spring官网:http://spring.io/docs

其他博文:有点多,感谢各位开源世界的大神!






1 0
原创粉丝点击