SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在XML配置文件中引入JAVA配置文件 <import> 、<bean>

一、在xml中引入xml,用<import>

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4 xmlns:c="http://www.springframework.org/schema/c"
 5 xsi:schemaLocation="http://www.springframework.org/schema/beans
 6 http://www.springframework.org/schema/beans/spring-beans.xsd">
 7 www.it-ebooks.info
 8 62 C HAPTER 2 Wiring beans
 9 <import resource="cd-config.xml" />
10 <bean id="cdPlayer"
11 class="soundsystem.CDPlayer"
12 c:cd-ref="compactDisc" />
13 </beans>

 

二、在xml中引入java配置,用<bean>

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4 xmlns:c="http://www.springframework.org/schema/c"
 5 xsi:schemaLocation="http://www.springframework.org/schema/beans
 6 http://www.springframework.org/schema/beans/spring-beans.xsd">
 7 <bean class="soundsystem.CDConfig" />
 8 <bean id="cdPlayer"
 9 class="soundsystem.CDPlayer"
10 c:cd-ref="compactDisc" />
11 </beans>

 

或新建一个文件,引入所有配置文件,不管是xml还是java

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:c="http://www.springframework.org/schema/c"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans
6 http://www.springframework.org/schema/beans/spring-beans.xsd">
7 <bean class="soundsystem.CDConfig" />
8 <import resource="cdplayer-config.xml" />
9 </beans>

Whether I’m using JavaConfig or XML wiring, I often create a root configuration, as I’ve
shown here, that brings together two or more wiring classes and/or XML files. It’s in
this root configuration that I’ll also usually turn on component scanning (with either
<context:component-scan> or @ComponentScan )

posted @ 2016-03-02 14:45  shamgod  阅读(259)  评论(0编辑  收藏  举报
haha