a02-3 装配Bean-xml装配(spring实战)
2018-08-07 本文已影响0人
brusion
本篇内容
- 1、简单使用xml实现实例注入
- 2、使用构造器初始化Bean
- 3、使用setter属性初始化Bean
一、创建简单xml
1、创建接口类
public interface CompactDisc {
void play();
}
2、创建实现类
public class CDPlayer implements CompactDisc {
public void play() {
System.out.println(" cd-player play ... ... ");
}
}
3、创建配置文件
- 配置文件用来装配bean的最基本XML元素包含在spring-beans模式中,在这个xml文件中被定义为根命名空间
- <beans>是该模式中的一个元素,是所有Spring配置文件的根元素
<?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.xsd">
<bean id="cdPlayer" class="com.demo.CDPlayer"/>
</beans>
说明:
- bean id="cdPlayer" class="com.demo.CDPlayer"
- id:通过id名称可以获取到此java实体对象
- class:java实体类的全限定名
4、测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"file:src/main/resources/bean.xml"})
public class CDPlayerTest {
@Autowired
public CDPlayer cdPlayer;
@Test
public void getBea(){
cdPlayer.play();
}
}
说明:
- @ContextConfiguration:指定配置文件的路径
demo代码为:02-xml-bean
二、使用构造器初始化Bean
1、定义接口
public interface CompactDisc {
void play();
}
2、定义实现类
public class SgtPepper implements CompactDisc {
public void play() {
System.out.println("sgt-pepper play ... ... ");
}
}
2.1、通过构造方法注入java引用对象
2.1.1、constructor-arg方式配置构造方法
1、创建java类
public class CDPlayer {
private CompactDisc compactDisc;
public CDPlayer(CompactDisc compactDisc) {
this.compactDisc = compactDisc;
}
public void play() {
compactDisc.play();
}
}
说明:
CDPlayer(CompactDisc compactDisc){}:用于spring通过构造器方式注入数据
2、创建配置文件
<bean id="compactDisc" class="com.demo.SgtPepper"/>
<bean id="cdPlayer" class="com.demo.CDPlayer">
<constructor-arg ref="compactDisc"/>
</bean>
说明:
- constructor-arg:为通过spring方式给java实体构造器注入数据
- ref="compactDisc":注入的值为id为compactDisc的java实体对象
3、测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"file:src/main/resources/bean.xml"})
public class CDPlayerTest {
@Autowired
private CDPlayer cdPlayer;
@Test
public void getCDPlayer() {
cdPlayer.play();
}
}
2.1.2、通过c命名空间注入java实体对象
<bean id="compactDisc" class="com.demo.SgtPepper"/>
<bean id="cdPlayer_c" class="com.demo.CDPlayer" c:compactDisc-ref="compactDisc"/>
<bean id="cdPlayer_0" class="com.demo.CDPlayer" c:_0-ref="compactDisc"/>
说明:
1、c:compactDisc-ref="compactDisc"
- c代表命名空间前缀
- compactDisc:代表构造器参数名称
- -ref: 代表bean引用(这是一个命名的约定)
- "compactDisc":代表需要注入bean的id值
2、c:_0-ref="compactDisc"
- _0:因为xml不允许用数字作为属性第一个字符所以加了下划线,代表为构造器的第一个参数
完整测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"file:src/main/resources/bean.xml"})
public class CDPlayerTest {
@Autowired
private CDPlayer cdPlayer;
@Autowired
private CDPlayer cdPlayer_c;
@Autowired
private CDPlayer cdPlayer_0;
@Test
public void getCDPlayer() {
cdPlayer.play();
cdPlayer_c.play();
cdPlayer_0.play();
}
}
2.2、将数据注入到构造器中
2.2.1、注入基本数据类型
1、定义java实体类
public class MediaPlayer implements CompactDisc {
private String title;
private String desc;
public MediaPlayer(String title, String desc) {
this.title = title;
this.desc = desc;
}
public void play() {
System.out.println("paly " + title + " " + desc);
}
}
2、配置文件
<!--Spring类型数据注入-->
<bean id="mediaPlayer" class="com.demo.MediaPlayer">
<constructor-arg value="test-title"/>
<constructor-arg value="test-desc"/>
</bean>
<bean id="mediaPlayer_c" class="com.demo.MediaPlayer" c:title="test_title-c" c:desc="test_desc-c"/>
<bean id="mediaPlayer_0" class="com.demo.MediaPlayer" c:_0="test_title-0" c:_1="test_desc-1"/>
说明:
1、constructor-arg value="test-title"
- constructor-arg:给实体对象构造器注入数据
- value="test-title":给构造器注入的具体数据值
- (也可以使用ref方式引用数据)
2、c:title="test_title-c"
- c:代表命名空间的前缀
- title:代表构造器参数名称
- "test_title-c":代码构造器参数具体的值
3、c:_0="test_title-0"
- c:代表命名空间的前缀
- _0:代表构造器参数的索引(_0代表第一个参数)
- "test_title-0":代码构造器参数具体的值
3、测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"file:src/main/resources/bean.xml"})
public class MediaPlayerTest {
@Autowired
private MediaPlayer mediaPlayer;
@Autowired
private MediaPlayer mediaPlayer_c;
@Autowired
private MediaPlayer mediaPlayer_0;
@Test
public void getMediaPlayer() {
mediaPlayer.play();
mediaPlayer_0.play();
mediaPlayer_c.play();
}
}
2.2.2、注入集合数据类型
1、创建java实体类
public class ListCDPlayer implements CompactDisc {
private List<String> list;
public ListCDPlayer(List<String> list) {
this.list = list;
}
public void play() {
for (String desc : list) {
System.out.println(desc);
}
}
}
2、配置文件
<!--list参数构造方法配置-->
<bean id="listCDPlayer" class="com.demo.ListCDPlayer">
<constructor-arg>
<list>
<value>test-cdPlay-0</value>
<value>test-cdPlay-1</value>
<value>test-cdPlay-2</value>
</list>
</constructor-arg>
</bean>
<bean id="test-bean-1" class="java.lang.String" c:_0="test-bean-1"/>
<bean id="test-bean-2" class="java.lang.String" c:_0="test-bean-2"/>
<bean id="test-bean-3" class="java.lang.String" c:_0="test-bean-3"/>
<bean id="test-bean-4" class="java.lang.String" c:_0="test-bean-4"/>
<bean id="listCDPlayer_ref" class="com.demo.ListCDPlayer">
<constructor-arg>
<list>
<ref bean="test-bean-1"/>
<ref bean="test-bean-2"/>
<ref bean="test-bean-3"/>
<ref bean="test-bean-4"/>
</list>
</constructor-arg>
</bean>
说明:
<constructor-arg>
<list>
<value>test-cdPlay-0</value>
<value>test-cdPlay-1</value>
<value>test-cdPlay-2</value>
</list>
</constructor-arg>
- 1、list元素是 constructor-arg的子元素,表名一个包含值的列表将会传递到构造器中
- 2、value元素是用来指定列表中的每一个元素
- 3、<ref bean="test-bean-1"/>是通过引用的方式获取数据
- 4、使用set集合时,参照list集合将<llist>元素替换成<set>
3、测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"file:src/main/resources/bean.xml"})
public class ListCDPlayerTest {
@Autowired
private ListCDPlayer listCDPlayer;
@Autowired
private ListCDPlayer listCDPlayer_ref;
@Test
public void getList() {
listCDPlayer.play();
}
@Test
public void getListRef() {
listCDPlayer_ref.play();
}
}
使用构造器初始化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:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--一个参数构造方法配置-->
<bean id="compactDisc" class="com.demo.SgtPepper"/>
<bean id="cdPlayer" class="com.demo.CDPlayer">
<constructor-arg ref="compactDisc"/>
</bean>
<bean id="cdPlayer_c" class="com.demo.CDPlayer" c:compactDisc-ref="compactDisc"/>
<bean id="cdPlayer_0" class="com.demo.CDPlayer" c:_0-ref="compactDisc"/>
<!--Spring类型数据注入-->
<bean id="mediaPlayer" class="com.demo.MediaPlayer">
<constructor-arg value="test-title"/>
<constructor-arg value="test-desc"/>
</bean>
<bean id="mediaPlayer_c" class="com.demo.MediaPlayer" c:title="test_title-c" c:desc="test_desc-c"/>
<bean id="mediaPlayer_0" class="com.demo.MediaPlayer" c:_0="test_title-0" c:_1="test_desc-1"/>
<!--list参数构造方法配置-->
<bean id="listCDPlayer" class="com.demo.ListCDPlayer">
<constructor-arg>
<list>
<value>test-cdPlay-0</value>
<value>test-cdPlay-1</value>
<value>test-cdPlay-2</value>
</list>
</constructor-arg>
</bean>
<bean id="test-bean-1" class="java.lang.String" c:_0="test-bean-1"/>
<bean id="test-bean-2" class="java.lang.String" c:_0="test-bean-2"/>
<bean id="test-bean-3" class="java.lang.String" c:_0="test-bean-3"/>
<bean id="test-bean-4" class="java.lang.String" c:_0="test-bean-4"/>
<bean id="listCDPlayer_ref" class="com.demo.ListCDPlayer">
<constructor-arg>
<list>
<ref bean="test-bean-1"/>
<ref bean="test-bean-2"/>
<ref bean="test-bean-3"/>
<ref bean="test-bean-4"/>
</list>
</constructor-arg>
</bean>
</beans>
三、采用setter设置属性方式
- 强依赖的属性采用构造器方式注入,可选性的依赖采用属性注入
1、创建接口类
public interface CompactDisc {
void play();
}
2、创建实现类
public class SgtPepper implements CompactDisc {
public void play() {
System.out.println("sgt-pepper play ... ... ");
}
}
3.1、注入自定义对象
1、创建接口实现类
public class CDPlayer {
private CompactDisc compactDisc;
public void play() {
compactDisc.play();
}
public void setCompactDisc(CompactDisc compactDisc) {
this.compactDisc = compactDisc;
}
}
说明:
- 定义setter方法用于给spring注入CompactDisc对象
2、添加配置文件
<bean id="sgtPepper" class="com.demo.SgtPepper"/>
<bean id="cdPlayer" class="com.demo.CDPlayer">
<property name="compactDisc" ref="sgtPepper"/>
</bean>
<bean id="cdPlayer_p" class="com.demo.CDPlayer" p:compactDisc-ref="sgtPepper"/>
说明:
1、property name="compactDisc" ref="sgtPepper"/>
- property元素为属性的setter方法所提供的功能
- name="compactDisc":表示属性的名称(setCompactDisc()去掉set,将字母首字母小写)
- ref="sgtPepper":表示给属性注入的值
2、p:compactDisc-ref="sgtPepper"
- p:命名空间前缀
- compactDisc:属性名称
- -ref:注入bean引用
- "sgtPepper":注入对应的id值
3、测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"file:src/main/resources/bean.xml"})
public class CDPlayerTest {
@Autowired
private CDPlayer cdPlayer;
@Autowired
private CDPlayer cdPlayer_p;
@Test
public void getCDPlay() {
cdPlayer.play();
cdPlayer_p.play();
}
}
3.2、注入Spring类型
1、定义接口实现类
public class MediaPlayer implements CompactDisc {
private String title;
private String desc;
public void play() {
System.out.println("Media-player play" + title + " and " + desc);
}
public void setTitle(String title) {
this.title = title;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
2、定义配置文件
<!--setter配置基本数据类型参数-->
<bean id="mediaPlay" class="com.demo.MediaPlayer">
<property name="desc" value="test-desc"/>
<property name="title" value="test-title"/>
</bean>
<bean id="title" class="java.lang.String" c:_0="bean-title"/>
<bean id="desc" class="java.lang.String" c:_0="bean-desc"/>
<bean id="mediaPlay_ref" class="com.demo.MediaPlayer">
<property name="title" ref="title"/>
<property name="desc" ref="desc"/>
</bean>
说明:
<property name="desc" value="test-desc"/>
直接设置Spring的值
name="desc":需要设置的属性名称
value="test-desc":属性对应的值
<property name="title" ref="title"/>
name="title" :需要设置的属性名称
ref="title":引用的id值(id必须存在)
3、定义测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"file:src/main/resources/bean.xml"})
public class MediaPlayerTest {
@Autowired
private MediaPlayer mediaPlay;
@Autowired
private MediaPlayer mediaPlay_ref;
@Test
public void getMedia(){
mediaPlay.play();
}
@Test
public void getMediaRef(){
mediaPlay_ref.play();
}
}
3.3、通过setter设置集合属性
1、定义接口实现类
public class ListCDPlayer implements CompactDisc {
private List<String> list;
public void play() {
for (String data : list) {
System.out.println(data);
}
}
public void setList(List<String> list) {
this.list = list;
}
}
2、定义配置文件
<!--集合配置-->
<bean id="listCDPlayer" class="com.demo.ListCDPlayer">
<property name="list">
<list>
<value>list-data-2</value>
<value>list-data-3</value>
<value>list-data-4</value>
</list>
</property>
</bean>
<bean id="list-bean-1" class="java.lang.String" c:_0="list-bean-1"/>
<bean id="list-bean-2" class="java.lang.String" c:_0="list-bean-2"/>
<bean id="list-bean-3" class="java.lang.String" c:_0="list-bean-3"/>
<bean id="listCDPlayer_ref" class="com.demo.ListCDPlayer">
<property name="list">
<list>
<ref bean="list-bean-1"/>
<ref bean="list-bean-2"/>
<ref bean="list-bean-3"/>
</list>
</property>
</bean>
<util:list id="list">
<value>utils-data-1</value>
<value>utils-data-2</value>
<value>utils-data-3</value>
<value>utils-data-4</value>
</util:list>
<bean id="listCDPlayer_util" class="com.demo.ListCDPlayer" p:list-ref="list"/>
说明:
1、 value>list-data-2</value
- value为list中每一个索引对应的值
2、ref bean="list-bean-3"
- ref为list中每一个索引引用对应的值
3、util:list id="list"
- util:list 元素会创建一个列表的bean
- p:list-ref="list":引用列表bean对象
3、测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"file:src/main/resources/bean.xml"})
public class ListCDPlayerTest {
@Autowired
private ListCDPlayer listCDPlayer;
@Autowired
private ListCDPlayer listCDPlayer_ref;
@Autowired
private ListCDPlayer listCDPlayer_util;
@Test
public void getList(){
listCDPlayer.play();
}
@Test
public void getListRef(){
listCDPlayer_ref.play();
}
@Test
public void getUtil(){
listCDPlayer_util.play();
}
}
使用setter属性配置完整配置文件
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="sgtPepper" class="com.demo.SgtPepper"/>
<bean id="cdPlayer" class="com.demo.CDPlayer">
<property name="compactDisc" ref="sgtPepper"/>
</bean>
<bean id="cdPlayer_p" class="com.demo.CDPlayer" p:compactDisc-ref="sgtPepper"/>
<!--setter配置基本数据类型参数-->
<bean id="mediaPlay" class="com.demo.MediaPlayer">
<property name="desc" value="test-desc"/>
<property name="title" value="test-title"/>
</bean>
<bean id="title" class="java.lang.String" c:_0="bean-title"/>
<bean id="desc" class="java.lang.String" c:_0="bean-desc"/>
<bean id="mediaPlay_ref" class="com.demo.MediaPlayer">
<property name="title" ref="title"/>
<property name="desc" ref="desc"/>
</bean>
<!--集合配置-->
<bean id="listCDPlayer" class="com.demo.ListCDPlayer">
<property name="list">
<list>
<value>list-data-2</value>
<value>list-data-3</value>
<value>list-data-4</value>
</list>
</property>
</bean>
<bean id="list-bean-1" class="java.lang.String" c:_0="list-bean-1"/>
<bean id="list-bean-2" class="java.lang.String" c:_0="list-bean-2"/>
<bean id="list-bean-3" class="java.lang.String" c:_0="list-bean-3"/>
<bean id="listCDPlayer_ref" class="com.demo.ListCDPlayer">
<property name="list">
<list>
<ref bean="list-bean-1"/>
<ref bean="list-bean-2"/>
<ref bean="list-bean-3"/>
</list>
</property>
</bean>
<util:list id="list">
<value>utils-data-1</value>
<value>utils-data-2</value>
<value>utils-data-3</value>
<value>utils-data-4</value>
</util:list>
<bean id="listCDPlayer_util" class="com.demo.ListCDPlayer" p:list-ref="list"/>
</beans>
4、spring-util命名空间中的元素
- util:constant(引用某个类型的public statis域,并暴露为bean)
- util:list(创建一个list类型的bean)
- util:map(创建一个map类型的bean,其中包含值或引用)
- util:properties(引用一个Properties类型的bean)
- util:property-path(引用一个bean的属性,并将其暴露为bean)
- util:set (创建一个set类型的bean,包含值或引用)