//spring的配置文件applicationContext.xml中:
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}"
p:pool-config-ref="poolConfig" />
报错信息:
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 102 in XML document from class path resource [springMVC.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 102; columnNumber: 36; 与元素类型 "bean" 相关联的属性 "p:host-name" 的前缀 "p" 未绑定。
原因:
使用<property>元素为Bean的属性装配值和引用并不太复杂。尽管如此,Spring的命名空间p提供了另一种Bean属性的装配方式,该方式不需要配置如此多的尖括号。
命名空间p的schemaURI为:
http://www.springframework.org/schema/p
如果你想使用命名空间p,只需要在Spring的XML配置中增加如下一段声明:
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"//加上这句话
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
通过此声明,我们现在可以使用p:作为<bean>元素所有属性的前缀来装配Bean的属性。为了示范,我们重新声明了kennyBean的配置:
<beanid="kenny"class="com.springinaction.springidol.Instrumentalist"
p:song="JingleBells"
p:instrument-ref="saxophone"/>
p:song属性的值被设置为“JingleBells”,将使用该值装配song属性。
同样,p:instrument-ref属性的值被设置为“saxophone”,将使用一个ID为saxophone的Bean引用来装配instrument属性。-ref后缀作为一个标识来告知Spring应该装配一个引用而不是字面值。
选择<property>还是命名空间p取决于你,它们是等价的。命名空间p的最主要优点是更简洁。在固定宽度的纸张上编写样例时,选择命名空间相对更合适。
参考:http://book.2cto.com/201306/25123.html