a02-3 装配Bean-xml装配(spring实战)

2018-08-07  本文已影响0人  brusion

本篇内容

一、创建简单xml

1、创建接口类

public interface CompactDisc {
    void play();
}

2、创建实现类

public class CDPlayer implements CompactDisc {
    public void play() {
        System.out.println(" cd-player play ... ... ");
    }
}

3、创建配置文件

<?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>

说明:

4、测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"file:src/main/resources/bean.xml"})
public class CDPlayerTest {
    @Autowired
    public CDPlayer cdPlayer;
    @Test
    public void getBea(){
        cdPlayer.play();
    }
}

说明:

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>
说明:
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"
2、c:_0-ref="compactDisc"
完整测试类
@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"
2、c:title="test_title-c"
3、c:_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>
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;
    }
}

说明:

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"/>
2、p:compactDisc-ref="sgtPepper"

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
2、ref bean="list-bean-3"
3、util:list id="list"

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命名空间中的元素

上一篇下一篇

猜你喜欢

热点阅读