上章讲了spring的入门,其中基于xml方式创建对象,在创建对象的时候,默认是执行无参构造方法完成对象创建
本章将讲解基于xml方式注入属性即

DI:依赖注入,对象属性的注入

准备工作

创建Book,Pen实体类,以及测试类,配置文件
Book实体类

//省略get set方法
private String bookName;
    private String author;

    private String[] arrays;
    private List<String> list;
    private Map<String,String> map;
    private Set<String> set;

    private List<Pen> pens;

    private Pen pen;

Pen实体类

//省略get set方法
private String bookName;
    private String penColor;
    public void write(){
        System.out.println("开始写书");
    }

测试类

public class spring5Test {

    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        Book book = context.getBean("book", Book.class);
    	//测试代码
	..........
    }

SET方法注入

在配置文件中配置属性注入

<bean id="book" class="life.wangquanguo.spring5.Book">
        <property name="author" value="刘慈欣"></property>
        <property name="bookName" value="三体"></property>
</bean>

使用有参构造方法注入

<bean id="book" class="life.wangquanguo.spring5.Book">
        <constructor-arg name="author" value="刘慈欣"></constructor-arg>
        <constructor-arg name="bookName" value="三体"></constructor-arg>
</bean>

此处也可以用index属性代替name属性注入

使用P名称空间注入(了解)

首先引入p名称空间的约束

xmlns:p="http://www.springframework.org/schema/p"

再在配置文件中配置

<bean id="book" class="life.wangquanguo.spring5.Book" p:bookName="三体" p:author="刘慈欣"></bean>

其他类型属性注入

NULL值注入

<bean id="book" class="life.wangquanguo.spring5.Book">
        <property name="bookName"><null/></property>
        <property name="author" value="无名氏"></property>
</bean>

特殊符号注入

<bean id="book" class="life.wangquanguo.spring5.Book">
        <property name="bookName"><value><![CDATA[<<三体>>]]></value></property>
        <property name="author" value="刘慈欣"></property>
</bean>

其中属性值包含特殊符号

  • 把<>进行转义 < >
  • 把带特殊符号内容写到 CDATA

外部bean注入

<bean id="book" class="life.wangquanguo.spring5.Book">
        <property name="pen" ref="pen"></property>
</bean>
<bean id="pen" class="life.wangquanguo.spring5.Pen"></bean>

其中ref属性就是其他对象 bean 标签 id 值

内部bean注入

<bean class="life.wangquanguo.spring5.Book" id="book">
	<property name="pen">
            <bean id="pen" class="life.wangquanguo.spring5.Pen"></bean>
        </property>
</bean>

级联赋值方式注入

<bean class="life.wangquanguo.spring5.Book" id="book">
        <property name="pen" ref="pen"></property>
        <property name="pen.penColor" value="红色"></property>
</bean>
<bean id="pen" class="life.wangquanguo.spring5.Pen"></bean>

注意要先引入类,否则会报错

集合属性的注入

基本类型

<bean id="book" class="life.wangquanguo.spring5.Book">
        <property name="arrays">
            <array >
                <value>1</value>
                <value>2</value>
            </array>
        </property>
        <property name="list">
            <list>
                <value>1</value>
                <value>2</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="1" value="1"></entry>
                <entry key="2" value="2"></entry>
            </map>
        </property>
        <property name="set">
            <set>
                <value>1</value>
                <value>2</value>
            </set>
	</property>
</bean>

引用类型

<bean id="pen1" class="life.wangquanguo.spring5.Pen" p:penColor="红色"></bean>
<bean id="pen2" class="life.wangquanguo.spring5.Pen" p:penColor="蓝色"></bean>
<bean class="life.wangquanguo.spring5.Book" id="book">
        <property name="pens">
            <list>
                <ref bean="pen1"/>
                <ref bean="pen2"/>
            </list>
        </property>
</bean>

提取集合注入部分

为了提高引用类型集合的复用,可以把集合提取出来
引入util名称空间

xmlns:util="http://www.springframework.org/schema/util"

在xsi:schemaLocation添加约束

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd

使用util标签完成list集合注入提取

<bean id="pen1" class="life.wangquanguo.spring5.Pen" p:penColor="红色"></bean>
<bean id="pen2" class="life.wangquanguo.spring5.Pen" p:penColor="蓝色"></bean>
<util:list id="penList">
        <ref bean="pen1"/>
        <ref bean="pen2"/>
</util:list>
<bean id="book" class="life.wangquanguo.spring5.Book">
        <property name="pens" ref="penList"></property>
</bean>

Q.E.D.

知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议