Spring-IoC-DI-基于xml的依赖注入-使用set方法进行注入(案例十三:使用p名称空间注入基本数据类型属性)
1.先导入p名称空间
xmlns:p="http://www.springframework.org/schema/p"
(1)创建对象
public class Book { 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)配置文件
<?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:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" 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"> <!-- P名称空间注入 --> <bean id="book" class="com.orz.spring.test1.Book" p:bname="蛙" p:bauthor="莫言"></bean> </beans>
(3)测试
@Test public void test1() { ApplicationContext context=new ClassPathXmlApplicationContext("bean13.xml"); Book book = context.getBean("book", Book.class); System.out.println(book); }
(4)结果
Book{bname='蛙', bauthor='莫言'}