介绍
在Spring中,对象无需自己查找或创建与其所关联的其他对象,容器负责把需要相互协作的对象引用赋予各个对象。创建应用对象之间协作关系的行为通常称为装配(wiring),这也是依赖注入的本质。Spring提供了四种主要的装配机制:
- 隐式的bean发现机制和自动装配
- 在java中进行显示配置(javaConfig)
- 在XML中进行显示配置
- 混合配置
例子程序中通用的接口
CD接口:
1 | public interface CompactDisc { |
CDPlayer接口:
1 | public interface MediaPlayer { |
自动化装配bean
Spring从两个角度来实现自动化装配:
- 组件扫描(component scanning):Spring 会自动发现应用上下文中创建的bean。
- 自动装配(autowiring):Spring自动满足bean之间的依赖。
带有@Component注解的
1 | /* |
@ComponentScan注解启用扫描组件
1 |
|
@Autowired注解实现自动装配:让Spring自动满足bean依赖
1 |
|
验证自动装配
1 | /* |
通过java代码装配bean
创建配置类:
1 | /* |
实现类
通过XML装配bean
XML配置规范:
1 | <?xml version="1.0" encoding="UTF-8"?> |
通用实现类和测试类:
1 | /* |
下面进行说明XML配置的几种情况:
构造器注入bean引用
1
2
3
4
5<bean id="compactDisc" class="spring02.assemble.xml.SgtPeppers" />
<bean id="cdPlayer" class="spring02.assemble.xml.CDPlayer">
<constructor-arg name = "compactDisc" ref="compactDisc" />
</bean>将字面量注入到构造器中
1
2
3
4
5
6
7
8<bean id="compactDisc" class="spring02.assemble.xml.BlankDisc">
<constructor-arg value="Sgt. Pepper's Lonely Hearts Club Band" />
<constructor-arg value="The Beatles" />
</bean>
<bean id="cdPlayer" class="spring02.assemble.xml.CDPlayer">
<constructor-arg ref="compactDisc" />
</bean>通过构造器装配集合
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25<bean id="compactDisc" class="spring02.assemble.xml.collections.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>
<value>She's Leaving Home</value>
<value>Being for the Benefit of Mr. Kite!</value>
<value>Within You Without You</value>
<value>When I'm Sixty-Four</value>
<value>Lovely Rita</value>
<value>Good Morning Good Morning</value>
<value>Sgt. Pepper's Lonely Hearts Club Band (Reprise)</value>
<value>A Day in the Life</value>
</list>
</constructor-arg>
</bean>
<bean id="cdPlayer" class="spring02.assemble.xml.CDPlayer">
<constructor-arg ref="compactDisc" />
</bean>构造器注入bean引用 — c-命名模式
1
2
3
4
5
6<!-- c-命名模式:需要在beans中引入c -->
xmlns:c="http://www.springframework.org/schema/c"
<bean id="compactDisc" class="spring02.assemble.xml.SgtPeppers" />
<!-- c-命名模式:c指前缀,cd指构造器参数名,-ref指注入bean引用,value指要注入的ID -->
<bean id="cdPlayer" class="spring02.assemble.xml.CDPlayer" c:cd-ref="compactDisc" />将字面量注入到构造器中 — c-命名模式
1
2
3
4
5
6
7<!-- c-命名模式:可以用占位符表示参数的位置,xml中不允许数字作为属性的第一个字符,所以加_前缀 -->
<bean id="compactDisc" class="spring02.assemble.xml.BlankDisc"
c:_0="Sgt. Pepper's Lonely Hearts Club Band"
c:_1="The Beatles">
</bean>
<!-- c-命名模式:当只有一个时可以只写_ -->
<bean id="cdPlayer" class="spring02.assemble.xml.CDPlayer" c:_-ref="compactDisc" />属性注入bean引用
1
2
3
4
5
6
7
8<bean id="compactDisc" class="spring02.assemble.xml.BlankDisc">
<constructor-arg value="Sgt. Pepper's Lonely Hearts Club Band" />
<constructor-arg value="The Beatles" />
</bean>
<bean id="cdPlayer" class="spring02.assemble.xml.properties.CDPlayer">
<property name="compactDisc" ref="compactDisc" />
</bean>将字面量注入到属性中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26<bean id="compactDisc"
class="spring02.assemble.xml.properties.BlankDisc">
<property name="title" value="Sgt. Pepper's Lonely Hearts Club Band" />
<property name="artist" value="The Beatles" />
<property name="tracks">
<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>
<value>She's Leaving Home</value>
<value>Being for the Benefit of Mr. Kite!</value>
<value>Within You Without You</value>
<value>When I'm Sixty-Four</value>
<value>Lovely Rita</value>
<value>Good Morning Good Morning</value>
<value>Sgt. Pepper's Lonely Hearts Club Band (Reprise)</value>
<value>A Day in the Life</value>
</list>
</property>
</bean>
<bean id="cdPlayer" class="spring02.assemble.xml.properties.CDPlayer"
<property name="compactDisc" ref="compactDisc" />
</bean>属性注入bean引用 — p-命名模式(规则同c-命名模式)
1
2
3
4
5
6
7
8
9
10
11
12<!-- p-命名模式:需要在beans中引入p -->
xmlns:p="http://www.springframework.org/schema/p"
<bean id="compactDisc" class="spring02.assemble.xml.BlankDisc">
<constructor-arg value="Sgt. Pepper's Lonely Hearts Club Band" />
<constructor-arg value="The Beatles" />
</bean>
<!-- p-命名模式:p指前缀,compactDisc指属性名,-ref指注入bean引用,value指要注入的ID -->
<bean id="cdPlayer" class="spring02.assemble.xml.properties.CDPlayer"
p:compactDisc-ref="compactDisc">
</bean>将字面量注入到属性中 — p-命名模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25<bean id="compactDisc" class="spring02.assemble.xml.properties.BlankDisc"
p:title="Sgt. Pepper's Lonely Hearts Club Band"
p:artist="The Beatles">
<property name="tracks">
<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>
<value>She's Leaving Home</value>
<value>Being for the Benefit of Mr. Kite!</value>
<value>Within You Without You</value>
<value>When I'm Sixty-Four</value>
<value>Lovely Rita</value>
<value>Good Morning Good Morning</value>
<value>Sgt. Pepper's Lonely Hearts Club Band (Reprise)</value>
<value>A Day in the Life</value>
</list>
</property>
</bean>
<bean id="cdPlayer" class="spring02.assemble.xml.properties.CDPlayer"
p:compactDisc-ref="compactDisc">
</bean>将字面量注入到属性中 — util-命名模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24<bean id="compactDisc" class="spring02.assemble.xml.properties.BlankDisc"
p:title="Sgt. Pepper's Lonely Hearts Club Band"
p:artist="The Beatles"
p:tracks-ref="trackList">
</bean>
<util:list id="trackList">
<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>
<value>She's Leaving Home</value>
<value>Being for the Benefit of Mr. Kite!</value>
<value>Within You Without You</value>
<value>When I'm Sixty-Four</value>
<value>Lovely Rita</value>
<value>Good Morning Good Morning</value>
<value>Sgt. Pepper's Lonely Hearts Club Band (Reprise)</value>
<value>A Day in the Life</value>
</util:list>
<bean id="cdPlayer" class="spring02.assemble.xml.properties.CDPlayer"
p:compactDisc-ref="compactDisc" />
</bean>Spring util-命名空间中的元素
| 元素 | 描述 |
| :——————–: | :——————————–: |
| util:constant | 引用某个类型的public static域,并将其暴露为bean |
| util:list | 创建一个java.util.List类型的bean,其中包含值或引用 |
| util:map | 创建一个java.util.Map类型的bean,其中包含值或引用 |
| util:properties | 创建一个java.util.Properties类型的bean |
| util:properties-path | 引用一个bean的属性(或内嵌属性),并将其暴露为bean |
| util:set | 创建一个java.util.Set类型的bean,其中包含值或引用 |
导入和混合配置
通用实现类:
1 | public class BlankDisc implements CompactDisc { |
javaConfig中引入XML配置
配置类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18/*
* CDPlayer采用javaConfig方式
* xml需要将cd引进来
*/
public class CDPlayerConfig {
public CDPlayer cdPlayer(CompactDisc compactDisc) {
return new CDPlayer(compactDisc);
}
}
(CDPlayerConfig.class)
"classpath:springXML02/ImportXmlConfigTest-config.xml") (
public class SoundSystemConfig {
}测试类:
1
2
3
4
5
6
7
8
9
10
11(SpringJUnit4ClassRunner.class)
(classes=SoundSystemConfig.class)
public class ImportXmlConfigTest {
private MediaPlayer player;
public void play() {
player.play();
}
}xml文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15<!-- xm需要将cd引进来 -->
<bean id="compactDisc" class="spring02.assemble.mixdconfig.BlankDisc"
c:_0="Sgt. Pepper's Lonely Hearts Club Band"
c:_1="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>XML配置中引入javaConfig
配置类:
1
2
3
4
5
6
7
8
9
10/*
* CDConfig在XML文件中被引入进来
*/
public class CDConfig {
public CompactDisc compactDisc() {
return new SgtPeppers();
}
}测试类:
1
2
3
4
5
6
7
8
9
10
11
12(SpringJUnit4ClassRunner.class)
"classpath:springXML02/ImportJavaConfigSystemTest-config.xml") (
public class ImportJavaConfigSystemTest {
private MediaPlayer player;
public void play() {
player.play();
}
}xml文件:
1
2
3
4
5
6
7
8
9
10<!-- 使用第三方配置文件将两个组合起来,在xml中引入javaConfig -->
<bean class="spring02.assemble.mixdconfig.CDConfig" />
<import resource="ImportJavaConfigTest-config.xml" />
<!-- 第三方配置文件需要引入的xml文件ImportJavaConfigTest-config.xml -->
<bean id="cdPlayer" class="spring02.assemble.mixdconfig.CDPlayer"
c:cd-ref="compactDisc" />