Spring auto wire(自动装配) 的 五种方式

Spring  auto-wire的 五种方式:

1:no   默认的方式是不进行自动装配,通过手工设置ref 属性来进行装配bean
2:byName   通过参数名 自动装配,如果一个bean的name 和另外一个bean的 property 相同,就自动装配。
3:byType   通过参数的数据类型自动自动装配,如果一个bean的数据类型和另外一个bean的property属性的数据类型兼容,就自动装配
4:construct   构造方法中的参数通过byType的形式,自动装配。
5:autodetect   如果有默认的构造方法,通过 construct的方式自动装配,否则使用 byType的方式自动装配。用于spring2.5 ,spring3.0测试不通过,估计是废弃了。

一: auto-wire  : no(默认方式)

首先定义测试需要的类:
package com.myapp.core.autowire;

public class Book {
   public  String  toString(){
	   return   "I'm  a book, read  me......";
   }
}

package com.myapp.core.autowire;

public class Person {
  
	private  Book book;
	
	public  Person(Book book){
		this.book= book;
	}
	
	public void  setBook(Book book){
		this.book = book;
	}
	
	public  String  toString(){
		return  "I'm  a person  I want  to  read  a book   "+ book;
	}
	
	public Person(){
		
	}
}

默认方式配置:
<?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-3.0.xsd">

  <!-- more bean definitions for data access objects go here -->
  
  <bean id="person" class="com.myapp.core.autowire.Person">
   <property name="book" ref="book" />
  </bean>
  
  <bean  id="book" class="com.myapp.core.autowire.Book"></bean>
  
</beans>

编写测试类:
package com.myapp.core.autowire;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {
	
	 public static void main(String[] args) {
		
		 ApplicationContext  context  = new ClassPathXmlApplicationContext("resource/autowire.xml");
		 
		 Person   person =  (Person)context.getBean("person");
		 
		  System.out.println(person.toString());
	}
  
}

测试结果:
I'm  a person  I want  to  read  a book   I'm  a book, read  me......

二:通过byName 自动装配:

修改配置文件如下:
在这个例子中通过bean的属性名自动装配;因为这个 person bean的属性 book和配置文件中id为book的名字相同,所以Spring将通过setBook(Book book)自动装配。
 <!-- byName -->
  
  <bean id="person" class="com.myapp.core.autowire.Person" autowire="byName">
  
  </bean>
  
  <bean  id="book" class="com.myapp.core.autowire.Book"></bean>

运行测试类:依然得到如上测试结果。

三:通过byType自动装配:

在这个例子中通过bean的属性名的类型进行自动装配;因为 person bean属性 book的类型和配置文件中id为book的类型相同,。所以spring通过setBook(Book  book)自动装配。
修改配置文件如下;
  <!-- byType -->
  
  <bean id="person" class="com.myapp.core.autowire.Person"  autowire="byType"/>
  <bean  id="book" class="com.myapp.core.autowire.Book"></bean>

四:通过construct自动装配:

通过构造方法的参数的数据类型进行自动装配,在这个例子中因为person的构造方法参数是Book类型,和配置文件中的id为book的类型相同,所以spring通过Person(Book book)构造方法进行自动装配。
修改配置文件如下:
  <!-- construct -->
  
   <bean id="person" class="com.myapp.core.autowire.Person"  autowire="constructor"/>
  
  <bean  id="book" class="com.myapp.core.autowire.Book"></bean>

五:通过 autodetect 自动装配:

Spring 3.0配置的xml不能用于autodetect,spring3.0应该是去掉了这个功能,改用spring2.5配置可以顺利通过测试,

首先使用construct的自动装配形式进行装配,如果没有construct就通过 byType的形式进行自动装配。
<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-2.5.xsd">

 <bean id="person" class="com.myapp.core.autowire.Person"   autowire="autodetect" />
  <bean  id="book" class="com.myapp.core.autowire.Book"></bean>
  
</beans>
测试结果:
I'm  a person  I want  to  read  a book   I'm  a book, read  me......

总结:
自动装配不容易看出bean之间的引用关系,增加了阅读的复杂度,一般还是采用默认的方式手工进行配置,或者采用annotation的方式进行配置。


附加: 总的配置,一步步放开进行测试:
<?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-3.0.xsd"> -->

<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-2.5.xsd">
  <!-- more bean definitions for data access objects go here -->
  <!-- no -->
 <!--  <bean id="person" class="com.myapp.core.autowire.Person">
   <property name="book" ref="book" />
  </bean> -->
  
  <!-- byName -->
  
 <!--  <bean id="person" class="com.myapp.core.autowire.Person" autowire="byName">
  
  </bean>
   -->
  <!-- byType -->
  
 <!--  <bean id="person" class="com.myapp.core.autowire.Person"  autowire="byType"/> -->
  
  <!-- construct -->
  
  <!--  <bean id="person" class="com.myapp.core.autowire.Person"  autowire="constructor"/> -->
  
  <!-- autodetect -->
  
  <bean id="person" class="com.myapp.core.autowire.Person"   autowire="autodetect" />
  <bean  id="book" class="com.myapp.core.autowire.Book"></bean>
  
</beans>






  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值