Announcement Announcement Module
Collapse
No announcement yet.
I can not get a bean from the Spring 2.5 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schem...-beans-2.5.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-2.0.2.xsd">

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

    </beans:beans>



    //Error shown from running on the JUnit Test.
    org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'myObject' is defined
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.getBeanDefinition(DefaultListab leBeanFactory.java:387)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getMergedLocalBeanDefinition(AbstractB eanFactory.java:96
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:246 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.context.support.AbstractApplic ationContext.getBean(AbstractApplicationContext.ja va:881)
    at config.SpringAccess.getClassTest(SpringAccess.java :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.

    Comment


    • #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).

      Comment


      • #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:

        Code:
        <listener>
        	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
        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.

        Comment

        Working...
        X