简化Spring XML配置

       在XML配置Spring中,可以通过<constructor-arg>和<property>元素装配bean,但是有时候,这样的装配并没有二义性。于是就可以使用Spring的自动装配来简化XML文件了。在Spring不能唯一的确定一个可以装配的Bean时,Spring并不会做太过积极的猜测,而是直接报错,抛出异常。这些简化手段,有三种形式。

 

(1)使用xml属性:1)byName;2)byType;3)constructor;4)autodetect;

byName通过把与装配属性具有相同名字(bean ID)的bean自动装配到Bean的对应属性中。

byType把与和要转配的bean具有相同属性的bean装配进去。

constructor把与构造器具有相同属性的Bean装配到构造器对应属性中。

autodetect先尝试construct在尝试byType

 

<bean id="instrument" class="org.spring.chapter3.Saxophone" />

<bean id="kenny"
	class="org.spring.chapter3.Instrumentalist"
	autowire="byType" >
<property name="song" value="Jingle Bells" />
</bean>

 声明两个bean,一个id为instrument和一个kenny。这里是要用instrument来装配kenny的一个instrument属性,在kenny的属性标签中声明了byType的自动装配,这里在bean中所依靠的setInstrument方法。

 

(2)使用注解装配

使用注解自动装配于在xml中使用autowire属性自动装配没有太大的区别。但是使用注解方式允许更细粒度的自动装配。要启用Spring使用注解的装配要在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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	
	<context:annotation-config />
	
	<bean id="instruments" class="org.spring.chapter3.Piano" />
	<bean id="kenny"
		class="org.spring.chapter3.Instrumentalist" >
	<property name="song" value="Jingle Bells" />
	</bean>
	
</beans>

 相对于不使用注解,此xml文件在beans属性中增加了三行。并使用<context:annotation-config />启用了注解配置。这里声明了一个instrments的bean但是在此文件中,并没有写相应的注入语句,而是在bean的Instrument属性前使用了@Autowire注解来配置这个属性。这里使用的就是基于注解的自动装配了。

 

(3)基于Java的配置

基于java的配置并不能完全消除xml只是将xml尽量做到最简。在xml中已经没有bean的声明了,bean的声明被转移到,相应的声明bean的java文件中去了。但是xml文件在这里依然是必须的。而且要在xml文件中启用这个功能,相应的xml文件如下。使用此功能必须导入cglib的库,否则无法使用此功能。

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	
<context:component-scan 
	base-package="org.spring.chapter3" />
	
</beans>

 

 下面是用来代替bean声明的java文件

package org.spring.chapter3;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringConfig {
	
	@Bean
	public Instrument piano(){
		return new Piano();
		
	}
	@Bean
	public Performer kenny(){
		Instrumentalist kenny = new Instrumentalist();
		kenny.setSong("Jingle Bells");
		kenny.setInstrument(piano());
		return kenny;
	}

}

 这段代码声明了一个名为piano的bean和一个名为kenny的bean,并且在kenny中将piano作为其属性注入到kenny中了。最后返回kenny,可在java中使用相应的方法来获取该bean了。

 

 在这里只是按照自己的理解将spring自动配置的方法分类为三种,并无任何依据,只为便于自己理解记忆。且上面所列代码均是不完整,只列出个人感觉比较重要,对于理解使用这种装配方法重要的代码。

 

 

 

注:Spring实战(第三版)的学习笔记

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值