5、p名称空间注入(了解
(1〉使用p名称空间注入,可以简化基于xml 配置方式
第一步添加p名称空间在配置文件中
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--set注入属性-->
<bean id="book" class="com.gsafety.spring5.Book" p:bautor="金庸" p:bname="天龙八部">
</bean>
</beans>
测试:
@Test public void testDemo(){ //1、加载spring配置文件 ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean1.xml"); //2、获取配置创建的对象 Book book = applicationContext.getBean("book", Book.class); System.out.println(book); book.testDemo(); }
输出:
Book{bname='天龙八部', bautor='金庸'}
testDemo......