Spring学习笔记—最小化Spring XML配置
自动装配(autowiring)有助于减少甚至消除配置<property>元素和<constructor-arg>元素,让Spring自动识别如何装配Bean的依赖关系。
自动检测(autodiscovery)比自动装配更进了一步,让Spring能够自动识别哪些类需要被配置成Spring Bean,从而减少对<bean>元素的使用。
<bean id = "kenny" class = "com.ouc.test.springs.Instruments" autowire = "byName" > <property name = "song" value = "bells" /> </bean>为属性自动装配ID与该属性的名字相同的Bean。
<bean id = "saxophone" class = "com.ouc.test.springs.Saxophone" primary = "false" />primary属性仅对标识首选Bean有意义。
<bean id = "saxophone" class = "com.ouc.test.springs.Saxophone" autowire-candidate = "false" />
<bean id = "saxophone" class="com.ouc.springs.test.Saxophone" autowire ="constructor" />
<bean id = "saxophone" class="com.ouc.springs.test.Saxophone" autowire ="autodetect" />
<?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.1.xsd default-autowire="byType" > </beans><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>
@Autowired public void setInstrument(Instrument instrument){ this.instrument = instrument; }Spring会尝试对该方法执行byType自动装配,可以使用@Autowired标注需要自动装配Bean引用的任意方法。
@Autowired private Instrument instrument;1)可选的自动装配:
@Autowired(required=false) private Instrument instrument;2)限定歧义性的依赖
@Autowired @Qualifier("guitar") private Instrument instrument;3)创建自定义的限定器(Qualifier)
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.beans.factory.annotation.Qualifier; @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Qualifier public @interface StringedInstrument{}
@Inject private Instrument instrument;@Inject没有required属性。
@Inject @Named("guitar") private Instrument instrument;@Named通过Bean的ID来标识可选择的Bean,@Named实际上是一个使用@Qualifier注解所标注的注解。
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import javax.inject.Qualifier; @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Qualifier public @interface StringedInstrument{}
@Value("#{systemProperties.myFavoriteSong}") private String song;
<?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.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/beans/spring-context-3.1.xsd" > <context:component-scan base-package="com.ouc.springs.test" > </context:component-scan> </beans>
<context:component-scan base-package="com.ouc.springs.test" > <context:include-filter type="assignable" expression="com.ouc.springs.tst.Instrument" /> <context:exclude-filter type="annotation" expression="com.ouc.springs.tst.SkipIt" /> </context:component-scan>
import org.springframework.context.annotation.Configuration; @Configuration public class SpringIdolConfig{}@Configuration注解会作为一个标识告知Spring:这个类将包含一个或多个Spring Bean的定义。
@Bean public Performer duke(){ return new Juggler(); }@Bean告知Spring这个方法将返回一个对象,该对象应该被注册为Spring应用上下文中的一个Bean,方法名将作为该Bean的ID。