Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Creating a ClassPathXmlApplicationContext

  1. #1

    Default Creating a ClassPathXmlApplicationContext

    I currently have a configuration file located in my classpath (C:/blah/blah/blah/spring.xml).

    I am trying to create an ApplicationContext using the ClassPathXmlApplicationContext in the following manner:

    Applicationcontext ctx = new ClassPathXmlApplicationContext("spring.xml");
    object = (object) ctx.getBean("MyService_MyAction");

    I continued to get an IOException "cannot be opened because it does not exist".

    After this, I modified the ClassPathXmlApplicationContext to use ("classpath*:spring.xml"). There is no IOException, but it is unable to create the bean:

    "NoSuchBeanDefinitionException: No Bean named 'MyService_MyAction' is defined.

    I have been able to load this spring.xml file and bean using the FileSysttemXmlApplicationContext, but I would like to have some more flexibility on where the spring config resides. Are there other options?

    Any help on this would be much appreciated.

    Thanks!
    Will

  2. #2
    Join Date
    Jul 2005
    Posts
    105

    Default

    Are you 100% certain that 'C:/blah/blah/blah' is on your classpath?

  3. #3
    Join Date
    Aug 2004
    Location
    New York
    Posts
    168

    Default

    Try

    Code:
    new ClassPathXmlApplicationContext("classpath*:/**/spring.xml");
    Or, since it's ClassPathXmlApplicationContext, I don't think you need the classpath prefix:

    Code:
    new ClassPathXmlApplicationContext("/**/spring.xml");
    Karl Baum
    weblog: www.jroller.com/page/kbaum

  4. #4

    Default

    I am developing on a PC and have added it to an environment variable called 'CLASSPATH'. Is there anything additional that I should modify?

  5. #5

    Default

    kbaum - thanks for the suggestions, but I am still receiving the "No bean name" exception.

    I am certain this bean exists as I have been able to load the same file and create the same bean using the FileSystemAppContext.

    Thanks,
    Will

  6. #6
    Join Date
    Aug 2004
    Location
    New York
    Posts
    168

    Default

    try to load:

    Code:
    Applicationcontext ctx = new ClassPathXmlApplicationContext("spring-not-exists.xml");
    object = (object) ctx.getBean("MyService_MyAction");
    You should get a resource not found exception. Then at least you know that in the other case spring is finding something.

    Are there any other beans defined in spring.xml? If so, are there any beans appearing your bean factory asside from "MyService_MyAction"?
    Karl Baum
    weblog: www.jroller.com/page/kbaum

  7. #7

    Default

    kbaum - I tried just that and as noted I got an IOException.

    By debugging I can see that there are no Bean Factories being created although there are several Beans defined in config. It seems that it is somehow missing all of the bean definitions, but IS finding the file.

    Is that possible?

    Thanks,
    Will

  8. #8
    Join Date
    Aug 2004
    Location
    New York
    Posts
    168

    Default

    Anything is possible but I'm not sure exactly what is going on. I would still want to verify that it is picking up the correct spring.xml. Try setting spring to debug log level. You might find some interesting information.

    If you don't find out anything interesting in the logs, i would probably resort to stepping through the creation of the bean factory within the debugger.
    Karl Baum
    weblog: www.jroller.com/page/kbaum

  9. #9

    Default

    To ensure that my file was in the classpath, i grabbed the system object and have pasted part of it below - this is the java.class.path object:

    C:\PVCS\Code\Common_Frameworks\src\frameworks\spri ng.xml;

    I want to use this file for my context so I am using the following:

    ApplicationContext ctx = new ClassPathXmlApplicationContext("/**/spring.xml");

    Although I am not getting a failure on this line, I continue to get an error on the following line:

    object = (object ) ctx.getBean("MyService_MyAction");

    "org.springframework.beans.factory.NoSuchBeanDefin itionException: No bean named 'MyService_MyAction' is defined"

    Any other ideas?

  10. #10
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,853

    Default

    Can you post the contents of the "spring.xml" file?

Posting Permissions

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