Results 1 to 2 of 2

Thread: Configuration Issues

  1. #1
    Join Date
    Jul 2009
    Posts
    1

    Default Configuration Issues

    Hi everyone,
    I am new to Spring Frameworks and am using eclipse IDE to run it.
    I tried a simple program....which seems to be giving some runtime exceptions when I run it.

    The config.xml is as follows
    <?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="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id = "firstbean"
    class = "InjectionBean"
    p:name = "andy"
    p:roll = "2121"
    p:dept = "Engg"
    p:college = "BU"/>

    </beans>

    My InjectionBean.java is as follows:
    public class InjectionBean {
    private String name, roll, dept, college;

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getRoll() {
    return roll;
    }

    public void setRoll(String roll) {
    this.roll = roll;
    }

    public String getDept() {
    return dept;
    }

    public void setDept(String dept) {
    this.dept = dept;
    }

    public String getCollege() {
    return college;
    }

    public void setCollege(String college) {
    this.college = college;
    }

    @Override
    public String toString() {
    return "InjectionBean [college=" + college + ", dept=" + dept + ", name="
    + name + ", roll=" + roll + ", toString()=" + super.toString()
    + "]";
    }

    }


    And I have a main class(Main.java) that runs this app.

    import org.springframework.beans.factory.xml.XmlBeanFacto ry;
    import org.springframework.core.io.ClassPathResource;


    public class Main {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    XmlBeanFactory beanXml = new XmlBeanFactory(new ClassPathResource("config.xml"));
    InjectionBean firstBean = (InjectionBean) beanXml.getBean("firstbean");
    System.out.println(firstBean);
    }

    }


    When I am running the main class.....as a java application...I am getting the following error/exception.

    Jul 8, 2009 6:35:47 PM org.springframework.beans.factory.xml.XmlBeanDefin itionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [config.xml]
    Exception in thread "main" org.springframework.beans.factory.BeanDefinitionSt oreException: IOException parsing XML document from class path resource [config.xml]; nested exception is java.io.FileNotFoundException: class path resource [config.xml] cannot be opened because it does not exist
    at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:349)
    at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:310)
    at org.springframework.beans.factory.xml.XmlBeanFacto ry.<init>(XmlBeanFactory.java:73)
    at org.springframework.beans.factory.xml.XmlBeanFacto ry.<init>(XmlBeanFactory.java:61)
    at Main.main(Main.java:12)
    Caused by: java.io.FileNotFoundException: class path resource [config.xml] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getI nputStream(ClassPathResource.java:142)
    at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:336)
    ... 4 more

    I have the commons-logging-1.0.1.jar and spring.jar included in my eclipse project. Any response in this regard shall be highly appreciated.

    regards,
    Rahul

  2. #2
    Join Date
    Jul 2009
    Posts
    1

    Default

    Try this instead of using classPathResource in the Main.java

    ApplicationContext context = new ClassPathXmlApplicationContext("pathtoconfig.xml") ;
    BeanFactory mybean = (BeanFactory) context;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •