0
点赞
收藏
分享

微信扫一扫

set注入专题2和p命名空间注入

set注入专题1接上  代码写在course5中

1. set注入专题之级联属性赋值  25

要点:

● 在spring配置文件中,如上,注意顺序。

● 在spring配置文件中,clazz属性必须提供getter方法

com.powernode.spring6.bean

Clazz

package com.powernode.spring6.bean;

/**
 * set注入专题之级联属性赋值  25
 * 表示班级
 **/
public class Clazz {
    // 班级名称
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String () {
        return "Clazz{" +
                "name='" + name + '\'' +
                '}';
    }
}

Student

package com.powernode.spring6.bean;

/**
 * set注入专题之级联属性赋值  25
 * 表示学生
 **/
public class Student {
    private String name;

    // 学生属于哪个班级
    private Clazz clazz;

    public void setClazz(Clazz clazz) {
        this.clazz = clazz;
    }

    // 使用级联属性赋值,这个需要这个get方法。
    public Clazz getClazz() {
        return clazz;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String () {
        return "Student{" +
                "name='" + name + '\'' +
                ", clazz=" + clazz +
                '}';
    }

}

<!--使用级联属性赋值需要注意两点:  25
            1. 配置的顺序不能颠倒,必须如下顺序。
            2. clazz属性必须提供getter方法。
    -->
    <bean id="studentBean" class="com.powernode.spring6.bean.Student">
        <!--简单类型,使用value-->
        <property name="name" value="张三"/>
        <!--这不是简单类型,使用ref-->
        <property name="clazz" ref="clazzBean"/>
        <!--级联属性赋值  25-->
        <property name="clazz.name" value="高三二班"/>
    </bean>

    <bean id="clazzBean" class="com.powernode.spring6.bean.Clazz"></bean>

    <!--<bean id="clazzBean" class="com.powernode.spring6.bean.Clazz">
        <property name="name" value="高三一班"/>
    </bean>-->

//set注入专题之级联属性赋值  25
    @Test
    public void testCascade(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("cascade.xml");

        Student studentBean = applicationContext.getBean("studentBean", Student.class);
        System.out.println(studentBean);

        Clazz clazzBean = applicationContext.getBean("clazzBean", Clazz.class);
        System.out.println(clazzBean);
    }

set注入专题2和p命名空间注入_List

2. set注入专题之注入数组  26

要点:

● 如果数组中是简单类型,使用value标签。

● 如果数组中是非简单类型,使用ref标签。

com.powernode.spring6.bean

Woman

package com.powernode.spring6.bean;

/**
 * set注入专题之注入数组  26
 **/
public class Woman {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String () {
        return "Woman{" +
                "name='" + name + '\'' +
                '}';
    }
}

QianDaYe

package com.powernode.spring6.bean;

import java.util.Arrays;

/**
 *  set注入专题之注入数组  26
 **/
public class QianDaYe {
    //String 是简单类型  26
    private String[] aiHaos;

    public void setAiHaos(String[] aiHaos) {
        this.aiHaos = aiHaos;
    }

    // 多个女性朋友 这个不是简单类型  26
    private Woman[] womens;

    public void setWomens(Woman[] womens) {
        this.womens = womens;
    }

    @Override
    public String () {
        return "QianDaYe{" +
                "aiHaos=" + Arrays.(aiHaos) +
                ", womens=" + Arrays.(womens) +
                '}';
    }
}

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

<!--    set注入专题之注入数组  26-->
    <bean id="w1" class="com.powernode.spring6.bean.Woman">
        <property name="name" value="小花"/>
    </bean>

    <bean id="w2" class="com.powernode.spring6.bean.Woman">
        <property name="name" value="小亮"/>
    </bean>

    <bean id="w3" class="com.powernode.spring6.bean.Woman">
        <property name="name" value="小明"/>
    </bean>

    <bean id="yuQian" class="com.powernode.spring6.bean.QianDaYe">
        <!-- 这个数组属性当中的元素类型是String简单类型 26-->
        <property name="aiHaos">
            <array>
                <value>抽烟</value>
                <value>喝酒</value>
                <value>烫头</value>
            </array>
        </property>

        <!-- 这个数组当中的类型就不是简单类型了-->
        <property name="womens">
            <array>
                <ref bean="w1"/>
                <ref bean="w2"/>
                <ref bean="w3"/>
            </array>
        </property>

    </bean>

</beans>

/set注入专题之注入数组  26
    @Test
    public void testArray(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-array.xml");
        QianDaYe yuQian = applicationContext.getBean("yuQian", QianDaYe.class);
        System.out.println(yuQian);
    }

set注入专题2和p命名空间注入_List_02

3. set注入专题之List和Set集合 27

List集合:有序可重复

注意:注入List集合的时候使用list标签,如果List集合中是简单类型使用value标签,反之使用ref标签。

Set集合:无序不可重复

要点:

● 使用标签

● set集合中元素是简单类型的使用value标签,反之使用ref标签

这里将代码和第4点写在一起

4.  set注入专题之Map和Properties   28

Map要点:

● 使用标签

● 如果key是简单类型,使用 key 属性,反之使用 key-ref 属性。

● 如果value是简单类型,使用 value 属性,反之使用 value-ref 属性。

Properties要点:

java.util.Properties继承java.util.Hashtable,所以Properties也是一个Map集合。

● 使用标签嵌套标签完成。

package com.powernode.spring6.bean;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * set注入专题之List和Set集合   27
 **/
public class Person {

    // 注入List集合
    private List names;

    // 注入Set集合
    private Set addrs;

    // 注入Map集合   28
    // 多个电话
    private Map phones;

    // 注入属性类对象  28
    // Properties本质上也是一个Map集合。
    // Properties的父类Hashtable,Hashtable实现了Map接口。
    // 虽然这个也是一个Map集合,但是和Map的注入方式有点像,但是不同。
    // Properties的key和value只能是String类型。
    private Properties properties;

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public void setPhones(Map phones) {
        this.phones = phones;
    }

    public void setNames(List names) {
        this.names = names;
    }

    public void setAddrs(Set addrs) {
        this.addrs = addrs;
    }

    @Override
    public String toString() {
        return "Person{" +
                "names=" + names +
                ", addrs=" + addrs +
                ", phones=" + phones +
                ", properties=" + properties +
                '}';
    }
}

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

<!--    set注入专题之List和Set集合   27-->
    <bean id="personBean" class="com.powernode.spring6.bean.Person">
        
        <property name="properties">
            <!--注入Properties属性类对象-->
            <props>
                <prop key="driver">com.mysql.cj.jdbc.Driver</prop>
                <prop key="url">jdbc:mysql://localhost:3306/spring6</prop>
                <prop key="username">root</prop>
                <prop key="password">123456</prop>
            </props>
        </property>

        <property name="phones">
            <!--注入Map集合   28-->
            <map>
                <!--如果key和value不是简单类型就用这个配置。-->
                <!--<entry key-ref="" value-ref=""/>-->
                <!--如果是简单类型就是key和value-->
                <entry key="1" value="110"/>
                <entry key="2" value="120"/>
                <entry key="3" value="119"/>
            </map>
        </property>

        <property name="names">
            <!--list集合有序可重复-->
            <list>
                <value>张三</value>
                <value>李四</value>
                <value>王五</value>
                <value>张三</value>
                <value>张三</value>
                <value>张三</value>
                <value>张三</value>
            </list>
        </property>

        <property name="addrs">
            <!--set集合无序不可重复-->
            <set>
                <value>北京大兴区</value>
                <value>北京大兴区</value>
                <value>北京海淀区</value>
                <value>北京海淀区</value>
                <value>北京大兴区</value>
            </set>
        </property>
    </bean>
</beans>

//set注入专题之List和Set集合   27
    //set注入专题之Map和Properties   28
    @Test
    public void testCollection(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-collection.xml");
        Person personBean = applicationContext.getBean("personBean", Person.class);
        System.out.println(personBean);
    }

set注入专题2和p命名空间注入_List_03

5. set注入专题之注入null和空字符串  29

5.1 注入空字符串   29

注入空字符串使用: 或者 value=""

● 我们先来看一下,怎么注入空字符串。

        <!--注入空字符串第一种方式-->
<!--        <property name="name" value=""/>-->
        <!--注入空字符串第二种方式-->
        <property name="name">
            <value/>
        </property>

5.2 怎么注入null呢?  29

注入null使用: 或者 不为该属性赋值

第一种方式:不给属性赋值,默认注入null

        <!--不给属性注入,属性的默认值就是null-->
        <!--<property name="name" value="tom"></property>-->
        <property name="age" value="20"/>

        <!-- 这不是注入null,这只是注入了一个"null"字符串-->
<!--        <property name="name" value="null"/>-->


第二种方式:使用

        <!--这种方式是手动注入null-->
        <property name="name">
            <null/>
        </property>

com.powernode.spring6.bean

Cat

package com.powernode.spring6.bean;

/**
 * @author 动力节点
 * @version 1.0
 * @className Cat
 * @since 1.0
 **/
public class Cat {
    private String name;
    private int age;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

set-di.xml

//set注入专题之注入null和空字符串  29
    @Test
    public void testNull(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("set-di.xml");
        Cat catBean = applicationContext.getBean("catBean", Cat.class);
        //System.out.println(catBean);
        System.out.println(catBean.getName().toUpperCase());
    }

6. set注入专题之注入特殊字符  30

com.powernode.spring6.bean

MathBean

package com.powernode.spring6.bean;

/**
 *  set注入专题之注入特殊字符  30
 **/
public class MathBean {
    private String result;

    public void setResult(String result) {
        this.result = result;
    }

    @Override
    public String toString() {
        return "MathBean{" +
                "result='" + result + '\'' +
                '}';
    }
}

XML中有5个特殊字符,分别是:<、>、'、"、&

以上5个特殊符号在XML中会被特殊对待,会被当做XML语法的一部分进行解析,如果这些特殊符号直接出现在注入的字符串当中,会报错。

set注入专题2和p命名空间注入_spring_04

6.1 第一种:特殊符号使用转义字符代替。  30

5个特殊字符对应的转义字符分别是:

特殊字符

转义字符

>

>

<

<

'

'

"

"

&

&

<!--        第一种方案:使用实体符号代替特殊符号-->
        <property name="result" value="2 < 3" />

//set注入专题之注入特殊字符  30
    @Test
    public void testSpecial(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("set-di.xml");
        MathBean mathBean = applicationContext.getBean("mathBean", MathBean.class);
        System.out.println(mathBean);
    }

set注入专题2和p命名空间注入_List_05

6.2 第二种:   30

将含有特殊符号的字符串放到: 当中。因为放在CDATA区中的数据不会被XML文件解析器解析。

注意:使用CDATA时,不能使用value属性,只能使用value标签

<!--第二种方案:使用<![CDATA[]]>-->
        <property name="result">
            <!--只能使用value标签-->
            <value><![CDATA[2 < 3]]></value>
        </property>

//set注入专题之注入特殊字符  30
    @Test
    public void testSpecial(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("set-di.xml");
        MathBean mathBean = applicationContext.getBean("mathBean", MathBean.class);
        System.out.println(mathBean);
    }

set注入专题2和p命名空间注入_set注入_06

举报

相关推荐

0 条评论