SpringMVC实践指南-第一集

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" /> 
构造器中使用Collections
public 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> 








	










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值