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......
博客介绍了使用p名称空间注入来简化基于xml的配置方式。通过在配置文件中添加p名称空间进行测试,输出了如Book{bname='天龙八部', bautor='金庸'}的结果。
1万+

被折叠的 条评论
为什么被折叠?



