user2154424
user2154424

Reputation: 31

error when trying to rewrite beans.xml using p-namespace in spring

MainApp48.java package com.tpoint;

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;
}
}

Finally 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>

This code works fine with the regular property tag in beans xml file. But I try to rewrite bean xml configuration using p-namespace I get this error. I can't figure out whats wrong

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)

Upvotes: 1

Views: 1350

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209004

"This code works fine with the regular property tag in beans xml file. But I try to rewrite bean xml configuration using p-namespace I get this error. I can't figure out whats wrong"

Looks like you haven't added the p namespace to your xml

xmlns:p="http://www.springframework.org/schema/p"

Upvotes: 4

Related Questions