Spring-IOC-基于XML的DI-注入字面量属性

Spring-IOC-基于XML的DI-注入字面量属性

1、创建Person类

package com.orz.spring.bean;
/**
 * @author orz
 * @create 2020-10-18 15:55
 */
public class Person {
    private String name;
    private String gender;
    private Integer age;

    public Person() {
    }

    public Person(String name, String gender, Integer age) {
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Integer getAge() {
        return age;
    }

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

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

2、注入null值

错误写法:

    <bean id="person" class="com.orz.spring.bean.Person">
        <property name="name" value="null"/>
        <property name="age" value="23"/>
        <property name="gender" value=""/>
    </bean>
@Test
    public void test1()
    {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
        Person person = applicationContext.getBean("person", Person.class);
        System.out.println(person.getName() == null);
    }
false

正确写法:

<bean id="person" class="com.orz.spring.bean.Person">
        <property name="name">
            <null/>
        </property>
        <property name="age" value="23"/>
        <property name="gender" value=""/>
    </bean>
@Test
    public void test1()
    {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
        Person person = applicationContext.getBean("person", Person.class);
        System.out.println(person.getName() == null);
    }
true

3、特殊字符

如:name=<<李华>>

方式一:使用 标签的 <![CDATA[ ]]>

   <bean id="person" class="com.orz.spring.bean.Person">
        <property name="name">
           <value><![CDATA[<<李华>>]]></value>
        </property>
        <property name="age" value="23"/>
        <property name="gender" value=""/>
    </bean>
 @Test
    public void test1()
    {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
        Person person = applicationContext.getBean("person", Person.class);
        System.out.println(person);
    }
Person{name='<<李华>>', gender='男', age=23}

方式二、使用xml转义字符

显示结果		描述	转义字符		十进制
 			空格	&nbsp;		&#160;
<			小于号	&lt;		&#60;
>			大于号	&gt;		&#62;
&			与号	&amp;		&#38;
"			双引号	&quot;		&#34;
'			单引号	&apos;		&#39;
×			乘号	&times;		&#215;
÷			除号	&divde;		&#247;
    <bean id="person" class="com.orz.spring.bean.Person">
        <property name="name" value="&lt;&lt;李华&gt;&gt;"/>
        <property name="age" value="23"/>
        <property name="gender" value="男"/>
    </bean>
@Test
    public void test1()
    {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
        Person person = applicationContext.getBean("person", Person.class);
        System.out.println(person);
    }
}
Person{name='<<李华>>', gender='男', age=23}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值