FreeMarkerConfigurer: This configurer registers a template loader for this package, allowing to reference the "spring.ftl" macro library.
自动装配bean:
(1)创建可被发现的bean: 使用@Component注解,设置bean名称
public interface Player{ }
@Component("beanname")
public class CDPlayer implements Player{
}
(2.1)打开组件扫描,创建配置类,使用标签@Configuration和@ComponentScan,设置扫描package,
@Configuration
@ComponentScan("packagename")
public class Config{
}
(2.2)使用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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="soundsystem" />
</beans>
(3)测试自动装配
一:设置上下文配置类,使用注解@ContextConfiguration(classes=Config.class)
二:使用@Autowired
@ContextConfiguration(classes=Config.class)
public class TestBean{
@Autowired
private Player player;
@Test
public void cdShouldNotBeNull(){
assertNotNull(player);
}
二:使用xml配置装配bean
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><bean id="compactDisc" class="soundsystem.SgtPeppers" />
构造器中使用其他bean引用
<bean id="cdPlayer" class="soundsystem.CDPlayer"> <constructor-arg ref="compactDisc" /> </bean>
构造器中使用字符串参数,例如
public BlankDisc(String title, String artist) { this.title = title; this.artist = artist; }
<bean id="compactDisc" class="soundsystem.BlankDisc"> <constructor-arg value="Sgt. Pepper's Lonely Hearts Club Band" /> <constructor-arg value="The Beatles" /> </bean> 或者使用
<bean id="compactDisc" class="soundsystem.BlankDisc" c:_title="Sgt. Pepper's Lonely Hearts Club Band" c:_artist="The Beatles" />
构造器中使用Collectionspublic BlankDisc(String title, String artist, List<String> tracks) { this.title = title; this.artist = artist; this.tracks = tracks; }
设置tracks为null
<bean id="compactDisc" class="soundsystem.BlankDisc"> <constructor-arg value="Sgt. Pepper's Lonely Hearts Club Band" /> <constructor-arg value="The Beatles" /> <constructor-arg><null/></constructor-arg> </bean>
或者<bean id="compactDisc" class="soundsystem.BlankDisc"> <constructor-arg value="Sgt. Pepper's Lonely Hearts Club Band" /> <constructor-arg value="The Beatles" /> <constructor-arg> <list> <value>Sgt. Pepper's Lonely Hearts Club Band</value> <value>With a Little Help from My Friends</value> <value>Lucy in the Sky with Diamonds</value> <value>Getting Better</value> <value>Fixing a Hole</value> <!-- ...other tracks omitted for brevity... --> </list> </constructor-arg> </bean>