Announcement Announcement Module
Collapse
No announcement yet.
NEWBIE Question: contextLoader not finding bean in context Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • NEWBIE Question: contextLoader not finding bean in context

    I've just started using Spring and I am trying to wire up a few beans. I have a bean in an applicationContext.xml that is referenced in my springapp-servlet.xml file.

    I get the error

    Error creating bean with name 'springAppController' defined in resource [/WEB-INF/springapp-servlet.xml
    ] of ServletContext: Can't resolve reference to bean 'someBeanManager' while sett
    ing property

    Do I have to do anything specific in code or in a file to allow the xx-servlet.xml file and the contextLoader to get the beans from the applicationContext.xml?

    THis is the definition in my applicationContext.xml

    Code:
        <bean id="someBeanDAO" class="com.test.dao.SomeBeanDaoImpl">
    		<property name="sessionFactory"><ref local="sessionFactory"/></property>
        </bean>
       
    	<bean id="someBeanManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager"><ref local="transactionManager"/></property>
    		<property name="target"><ref local="someBeanDAO"/></property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    				<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
    				<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
    				<prop key="store*">PROPAGATION_REQUIRED</prop>
    			</props>
    		</property>
    	</bean>
    And in my springapp-servlet.xml I have

    Code:
        <bean id="springAppController" class="com.test.web.SpringAppController">
    		<property name="someBeanManager"><ref bean="packageManager"/></property>        
        </bean>

  • #2
    Do I have to do anything specific in code or in a file to allow the xx-servlet.xml file and the contextLoader to get the beans from the applicationContext.xml?
    Yes, you have to include the applicationContext.xml in your contextConfigLocations parameter in web.xml. Have a look at web.xml from jPetstore (http://cvs.sourceforge.net/viewcvs.p...=1.9&view=auto) to see how it works.

    Code:
    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
          /WEB-INF/applicationContext.xml
      </param-value>
    </context-param>
    You also have to include a ContextLoaderListener or ContextLoaderServlet (Depending on you servlet container). Examples can all be found in the file mentioned above.

    Hope this helps.

    Comment


    • #3
      Where do you have this <ref bean="packageManager"/> specified?

      Should this be vice versa?
      <property name="packageManager"><ref bean="someBeanManager"/></property>

      Rgds, Ales

      Comment


      • #4
        Hi, I've included that in (prior to your message). It is being called because I see this in the logs.

        Code:
        2004-10-31 08&#58;30&#58;02,714 INFO &#91;org.springframework.beans.factory.xml.XmlBeanDefin
        itionReader&#93; - <Loading XML bean definitions from resource &#91;/WEB-INF/application
        Context.xml&#93; of ServletContext>
        2004-10-31 08&#58;30&#58;03,435 INFO &#91;org.springframework.web.context.support.XmlWebAppl
        icationContext&#93; - <Bean factory for application context &#91;Root XmlWebApplicationC
        ontext&#93;&#58; org.springframework.beans.factory.support.DefaultListableBeanFactory de
        fining beans &#91;dataSource,sessionFactory,transactionManager,someBeanDAO,someBeanManager&#93;; Root of BeanFactory hierarchy>
        2004-10-31 08&#58;30&#58;03,975 INFO &#91;org.springframework.web.context.support.XmlWebAppl
        icationContext&#93; - <5 beans defined in ApplicationContext &#91;Root XmlWebApplication
        Context&#93;>
        Later on I see the message that the shared instance has been created.

        Comment


        • #5
          Originally posted by alesj
          Where do you have this <ref bean="packageManager"/> specified?

          Should this be vice versa?
          <property name="packageManager"><ref bean="someBeanManager"/></property>

          Rgds, Ales
          Sorry, typo.

          They both say "someBeanManager"

          Comment


          • #6
            My web.xml looks as follows:

            Code:
            	<context-param>
            		<param-name>contextConfigLocation</param-name>
            		<param-value>/WEB-INF/applicationContext.xml</param-value>
            	</context-param>
            
            	<servlet>
            		<servlet-name>context</servlet-name>
            		<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
            		<load-on-startup>1</load-on-startup>
            	</servlet>
                
                
                <servlet>
                    <servlet-name>springapp</servlet-name>
                    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                    <load-on-startup>1</load-on-startup>
                </servlet>
            
                <servlet-mapping>
                    <servlet-name>springapp</servlet-name>
                    <url-pattern>*.html</url-pattern>
                </servlet-mapping>

            Comment


            • #7
              Found the problem. The dispatcher servlet was being loaded before the context loader. I switched that around and it works -- almost.

              Need to solve the "failed to convert property value" problem now.

              Comment

              Working...
              X