Spring-IoC-DI-基于xml的依赖注入-使用set方法进行注入(案例三:注入字面量类型属性(属性值包含特殊符号))

案例三:注入字面量类型属性(属性值包含特殊符号)

(1)创建类,定义属性和对应的set方法

public class Book3 {
private String bname;
private String bauthor;
public void setBname(String bname) {
this.bname = bname;
}
public void setBauthor(String bauthor) {
this.bauthor = bauthor;
}
@Override
public String toString() {
return "Book{" +
"bname='" + bname + '\'' +
", bauthor='" + bauthor + '\'' +
'}';
}
}

 

(2)在spring配置文件中先配置对象创建,再配置属性注入

<bean id="book3" class="com.orz.spring.test3.Book3">
<property name="bauthor" value="无名氏"></property>
<!-- 3.使用CDATA注入特殊符号 -->
<property name="bname">
<value><![CDATA[《易筋经》]]></value>
</property>
</bean>

(3)测试

    @Test
public void test1()
{
//1.加载spring配置文件
ApplicationContext context=new ClassPathXmlApplicationContext("bean3.xml");
//2.获取配置文件的创建对象
Book3 book3 = context.getBean("book3", Book3.class);
//3
System.out.println(book3);
}

(4)结果

Book{bname='《易筋经》', bauthor='无名氏'}

 

posted @ 2020-08-14 16:37  orz江小鱼  阅读(157)  评论(0编辑  收藏  举报