1
MainApp48.java package com.tpoint;嘗試在spring中使用p名稱空間重寫beans.xml時出錯
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp48 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"Beans48.xml");
Jhon obja = (Jhon) context.getBean("jhon");
System.out.println(obja);
System.out.println(obja.getName());
System.out.println(obja.getJane().getName());
}
}
Jhon.java
package com.tpoint;
public class Jhon {
private String name;
private Jane jane;
public void setName(String name)
{
this.name = name;
}
public void setJane(Jane jane)
{
this.jane = jane;
}
public String getName()
{
return this.name;
}
public Jane getJane()
{
return this.jane;
}
}
最後Beans48.xml
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema /beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<bean id ="jhon" class="com.tpoint.Jhon"
p:name = "test name"
p:jane-ref="jane" />
<bean id ="jane" class="com.tpoint.Jane" p:name="test jane name" />
</beans>
此代碼工作正常,在豆類xml文件的常規屬性標記。但我嘗試使用p-namespace重寫bean xml配置我得到這個錯誤。我無法弄清楚什麼是錯
Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 13 in XML document from class path resource [Beans48.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 13; columnNumber: 21; The prefix "p" for attribute "p:name" associated with an element type "bean" is not bound.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
感謝。它有幫助。 – user2154424