鍍金池/ 教程/ Java/ Spring自動(dòng)裝配Beans
Spring EL hello world實(shí)例
Spring使用@Autowired注解自動(dòng)裝配
MapFactoryBean實(shí)例
Bean InitializingBean和DisposableBean實(shí)例
Spring使用@Required注解依賴檢查
安裝Spring工具套件到Eclipse
CGLIB is required to process @Configuration classes
Spring EL Arrays, Lists, Maps實(shí)例
Spring由類型(Type)自動(dòng)裝配
Spring在bean配置文件中定義電子郵件模板
Spring發(fā)送帶附件郵件
ListFactoryBean實(shí)例
Struts 2 + Spring + Hibernate集成實(shí)例
Spring bean引用實(shí)例
Spring JdbcTemplate batchUpdate()實(shí)例
Spring SetFactoryBean實(shí)例
Spring通過setter方法注入
Spring EL hello world實(shí)例
在bean配置文件中的電子郵件模板
Spring發(fā)送帶附件的Email
Spring EL 方法調(diào)用實(shí)例
Spring自動(dòng)裝配Beans
Spring由構(gòu)造方法自動(dòng)裝配
Spring使用@Required注解依賴檢查
Spring SimpleJdbcTemplate查詢示例
自定義@Required-style注解
Spring+JDBC實(shí)例
Spring EL三元運(yùn)算(if-then-else)實(shí)例
JdbcTemplate+JdbcDaoSupport實(shí)例
Spring自動(dòng)掃描組件
Spring EL方法調(diào)用實(shí)例
Spring自動(dòng)裝配Beans
Spring EL bean引用實(shí)例
Spring依賴注入(DI)
Spring EL Lists,Maps實(shí)例
Spring通過構(gòu)造方法依賴注入
通過MailSender發(fā)送電子郵件
Spring AOP實(shí)例(Pointcut, Advisor)
Spring EL運(yùn)算符實(shí)例
Spring JavaConfig實(shí)例
Spring依賴注入 (DI)
Spring EL bean引用實(shí)例
Spring由AutoDetect自動(dòng)裝配
Spring由構(gòu)造方法自動(dòng)裝配
Spring JdbcTemplate查詢實(shí)例
Spring SimpleJdbcTemplate batchUpdate()實(shí)例
Spring+JDBC實(shí)例
如何注入值到Spring bean屬性
Spring SimpleJdbcTemplate類命名參數(shù)實(shí)例
Spring PropertyPlaceholderConfigurer實(shí)例
Spring JdbcTemplate batchUpdate() 實(shí)例
Spring EL與ExpressionParser測(cè)試
Spring內(nèi)部Bean實(shí)例
加載多個(gè)Spring bean配置文件
Spring+Hibernate+MySql實(shí)例(注解)
Spring AOP+AspectJ 在XML配置實(shí)例
Spring自動(dòng)代理創(chuàng)建者實(shí)例
Spring EL正則表達(dá)式實(shí)例
測(cè)試 Spring EL與ExpressionParser
java.lang.ClassNotFoundException: org.exolab.castor.xml.XMLExcep
Spring PropertyPlaceholderConfigurer實(shí)例
Spring bean配置繼承
Spring使用Setter依賴注入
Spring自動(dòng)裝配@Qualifier實(shí)例
Spring依賴檢查
Spring hello world實(shí)例
Spring依賴注入servlet會(huì)話監(jiān)聽器
Spring自定義@Required-style注解
Spring bean作用域
Spring AOP攔截器的序列
注入值到Spring Bean的屬性
Spring JdbcTemplate+JdbcDaoSupport實(shí)例
Spring教程
Spring自動(dòng)裝配@Qualifier實(shí)例
Spring MapFactoryBean例子
Spring Bean作用域?qū)嵗?/span>
Cannot proxy target class because CGLIB2 is not available
Spring AOP攔截器的順序
Spring集合(List, Set, Map, and Properties)實(shí)例
Spring SimpleJdbcTemplate類命名參數(shù)實(shí)例
Spring通過自動(dòng)檢測(cè)自動(dòng)裝配
Spring+Hibernate+MySql實(shí)例
Spring JavaConfig @Import實(shí)例
訪問MessageSource的bean(MessageSourceAware)
Spring JdbcTemplate查詢實(shí)例
Spring JavaConfig @Import實(shí)例
Spring Object/XML映射實(shí)例
Spring Bean引用例子
Spring AOP+AspectJ注解實(shí)例
java.lang.ClassNotFoundException: org.apache.xml.serialize.XMLSe
Spring依賴檢查
Spring JavaConfig實(shí)例
Spring AOP+AspectJ注解實(shí)例
Spring bean配置繼承
Spring自動(dòng)掃描組件
自動(dòng)代理創(chuàng)建者實(shí)例
Spring Bean init-method 和 destroy-method實(shí)例
Spring通過構(gòu)造方法注入
Spring過濾器組件自動(dòng)掃描
Spring構(gòu)造方法注入類型歧義
Spring+JDBC實(shí)例
Spring EL正則表達(dá)式實(shí)例
Spring通過Gmail SMTP服務(wù)器MailSender發(fā)送電子郵件
Spring AOP+AspectJ在XML配置實(shí)例
SetFactoryBean實(shí)例
Spring注入日期到bean屬性-CustomDateEditor
Spring AOP通知實(shí)例 – Advice
Spring使用@Autowired注解自動(dòng)裝配
Spring AOP實(shí)例(Pointcut,Advisor)
Spring AOP在Hibernate的事務(wù)管理
Spring初學(xué)快速入門
Spring內(nèi)部bean實(shí)例
Spring bean加載多個(gè)配置文件
Spring + Hibernate+ MySql注解實(shí)例
Spring @PostConstruct和@PreDestroy實(shí)例
Spring依賴注入servlet會(huì)話監(jiān)聽器
Spring ListFactoryBean實(shí)例
Spring松耦合的實(shí)例
Spring過濾器組件自動(dòng)掃描
Spring按名稱(Name)自動(dòng)裝配
Spring AOP通知實(shí)例(Advice)
Spring AOP在Hibernate事務(wù)管理
Bean init-method和destroy-method實(shí)例
Spring EL操作符實(shí)例
Spring注入日期到bean屬性(CustomDateEditor)
資源加載程序的getResource()示例
Bean @PostConstruct和@PreDestroy實(shí)例
Struts2+Spring集成實(shí)例
Spring集合 (List,Set,Map,Properties) 實(shí)例
Spring按類型(Type)自動(dòng)裝配
Spring由名稱(Name)自動(dòng)裝配
Spring SimpleJdbcTemplate batchUpdate()實(shí)例
Spring hello world實(shí)例
Spring Bean InitializingBean和DisposableBean實(shí)例
Spring構(gòu)造方法注入類型歧義
Spring EL三元操作符(if-then-else)實(shí)例
Spring+Hibernate+MySql實(shí)例
Spring松耦合實(shí)例

Spring自動(dòng)裝配Beans

在Spring框架,可以用 auto-wiring 功能會(huì)自動(dòng)裝配Bean。要啟用它,只需要在 <bean>定義“autowire”屬性。
<bean id="customer" class="com.yiibai.common.Customer" autowire="byName" />
在Spring中,支持 5 自動(dòng)裝配模式。
  • no – 缺省情況下,自動(dòng)配置是通過“ref”屬性手動(dòng)設(shè)定
  • byName – 根據(jù)屬性名稱自動(dòng)裝配。如果一個(gè)bean的名稱和其他bean屬性的名稱是一樣的,將會(huì)自裝配它。
  • byType – 按數(shù)據(jù)類型自動(dòng)裝配。如果一個(gè)bean的數(shù)據(jù)類型是用其它bean屬性的數(shù)據(jù)類型,兼容并自動(dòng)裝配它。
  • constructor – 在構(gòu)造函數(shù)參數(shù)的byType方式。
  • autodetect – 如果找到默認(rèn)的構(gòu)造函數(shù),使用“自動(dòng)裝配用構(gòu)造”; 否則,使用“按類型自動(dòng)裝配”。

示例

Customer 和 Person 對(duì)象自動(dòng)裝配示范。
package com.yiibai.common;

public class Customer 
{
	private Person person;
	
	public Customer(Person person) {
		this.person = person;
	}
	
	public void setPerson(Person person) {
		this.person = person;
	}
	//...
}
package com.yiibai.common;

public class Person 
{
	//...
}

1. Auto-Wiring ‘no’

這是默認(rèn)的模式,你需要通過 'ref' 屬性來連接 bean。
<bean id="customer" class="com.yiibai.common.Customer">
                  <property name="person" ref="person" />
	</bean>

	<bean id="person" class="com.yiibai.common.Person" />

2. Auto-Wiring ‘byName’

按屬性名稱自動(dòng)裝配。在這種情況下,由于對(duì)“person” bean的名稱是相同于“customer” bean 的屬性(“person”)名稱,所以,Spring會(huì)自動(dòng)通過setter方法將其裝配 – “setPerson(Person person)“.

<bean id="customer" class="com.yiibai.common.Customer" autowire="byName" />	
<bean id="person" class="com.yiibai.common.Person" />

查看完整的示例 – Spring按名稱自動(dòng)裝配

3. Auto-Wiring ‘byType’

通過按屬性的數(shù)據(jù)類型自動(dòng)裝配Bean。在這種情況下,由于“Person” bean中的數(shù)據(jù)類型是與“customer” bean的屬性(Person對(duì)象)的數(shù)據(jù)類型一樣的,所以,Spring會(huì)自動(dòng)通過setter方法將其自動(dòng)裝配。– “setPerson(Person person)“.

<bean id="customer" class="com.yiibai.common.Customer" autowire="byType" />
<bean id="person" class="com.yiibai.common.Person" />

查看完整的示例 – Spring通過類型自動(dòng)裝配

4. Auto-Wiring ‘constructor’

通過構(gòu)造函數(shù)參數(shù)的數(shù)據(jù)類型按屬性自動(dòng)裝配Bean。在這種情況下,由于“person” bean的數(shù)據(jù)類型與“customer” bean的屬性(Person對(duì)象)的構(gòu)造函數(shù)參數(shù)的數(shù)據(jù)類型是一樣的,所以,Spring通過構(gòu)造方法自動(dòng)裝配 – “public Customer(Person person)“.

<bean id="customer" class="com.yiibai.common.Customer" autowire="constructor" />
	
	<bean id="person" class="com.yiibai.common.Person" />

查看完整的示例 – 

<bean id=" customer"="" class="com.yiibai.common.Customer" autowire="autodetect" >="" <bean="" id="person" ><="" pre="">  
		

查看完整的示例 – Spring按AutoDetect自動(dòng)裝配成功.

這是一件好事,這兩個(gè)auto-wire’ 和 ‘dependency-check’ 相結(jié)合,以確保屬性始終自動(dòng)裝配成功。
<bean id="customer" class="com.yiibai.common.Customer" 
			autowire="autodetect" dependency-check="objects />
	
	<bean id="person" class="com.yiibai.common.Person" />

結(jié)論

在我看來,Spring ‘a(chǎn)uto-wiring’ 以極大的成本做出更快開發(fā)效率 - 它增加了對(duì)整個(gè) bean 配置文件復(fù)雜性,甚至不知道哪些bean將自動(dòng)裝配哪個(gè)Bean。

在實(shí)踐中,我更頃向手動(dòng)關(guān)聯(lián)創(chuàng)建,它始終是干凈,也很好地工作,或者使用 @Autowired 注解,這是更加靈活和建議。