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格式定义则不会出现这样问题

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值