Announcement Announcement Module
No announcement yet.
I can not get a bean from the Spring 2.5 Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • I can not get a bean from the Spring 2.5

    if you have condition to answer me, please do this.
    I can not read any bean from spring 2.5.
    I already have read magazines and articles on internet but I never have
    success. Can you help me.

    // I am using the IDE Eclipse Helios with JSF 2.0, TomCat 7.0, Hibernate 3.0 and Spring 2.5, but I think the problem is related to Spring.

    //Creating the applicationContext: (This part works very well)
    springContext = new ClassPathXmlApplicationContext("classpath*:src/config/applicationContext.xml");

    //Trying to create the object named myObject from Spring 2.5.
    //I could not read the bean from Spring.
    //The error says the myObject is not defined. But the myObject is.
    ClassTest myObject =(ClassTest) getSpringContext().getBean("myObject");

    //Configuration file: applicationContext.xml
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <beans:beans xmlns=""

    <!-- It is very important to say that the path and class are corrects and both exist.-->
    <beans:bean id="myObject" class="src.test.ClassTest" ></beans:bean>


    //Error shown from running on the JUnit Test.
    org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'myObject' is defined
    at istableBeanFactory.getBeanDefinition(DefaultListab
    at BeanFactory.getMergedLocalBeanDefinition(AbstractB
    at BeanFactory.doGetBean( )
    at BeanFactory.getBean(
    at BeanFactory.getBean(
    at ationContext.getBean(AbstractApplicationContext.ja va:881)
    at config.SpringAccess.getClassTest( :42)

    Thank you in advance,

    Régis de O. Santos(from BRAZIL)

  • #2
    If you're using Tomcat and JSF it means yours is a Web Application. For web applications, you DON'T load a classpath application context programmatically like you do. Instead, you load the web application context through the appropriate listener in web.xml. That is the correct way to do things, and it's throughly explained in the reference documentation; if you deviate from this path, you can't expect things to work properly.


    • #3
      I already have changed my program using other manner without to use the Path.
      I appreciate your help and I put other Message explaining again the same problem but using AnnotationConfigWebApplicationContext .
      I continue can not read the bean from the Spring 3.1.0.M1.
      I have sent other Thread. Now I am using AnnotationConfigWebApplicationContext?

      Thank you in advance. Régis de Oliveira Santos(From BRAZIL).


      • #4
        AnnotationConfigWebApplicationContext is used to enable JavaConfig elements and mix them with standard DI annotations. Your configuration is not xml-centric anymore but is driven through one or more annotated java files. This is pretty advanced stuff, for a beginner like yourself I would recommend the standard way to load application context for a web application. Just add the following lines to your web.xml deployment descriptor:

        and put your configuration in a file called exactly applicationContext.xml which you will put under /WEB-INF folder.

        If you activate log4j to debug level for org.springframework, you should see your application context being loaded correctly.