Spring使用p空间配置属性——基于XML Schema的简化配置

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束 -->
<!-- xmlns:p是导入XML Schema里的p名字空间 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	
	<!-- 配置chinese实例,其实现类是Chinese -->
	<bean id="chinese" class="tju.chc.app.service.impl.Chinese"
	p:axe-ref="stoneAxe" p:name="孙悟空"/>
	
	<!-- 与上面等效<bean id="chinese" class="tju.chc.app.service.impl.Chinese">
		<property name="axe" ref="stoneAxe"/>
		<property name="name" value="孙悟空"/>
	</bean>
	-->
	<!-- 配置stoneAxe实例,其实现类是StoneAxe -->	
	<bean id="stoneAxe" class="tju.chc.app.service.impl.StoneAxe"/>
	<!-- 配置steelAxe实例,其实现类是SteelAxe -->	
	<bean id="steelAxe" class="tju.chc.app.service.impl.SteelAxe"/>
</beans>

使用p空间没有标准的XML格式灵活,如果某个Bean的属性是以“-ref”结尾的,那么采用p名称空间定义时就会导致冲突,而采用标准XML格式定义则不会出现这样问题

你可能感兴趣的:(spring,xml,schema,p)