PDA

View Full Version : NEWBIE Question: contextLoader not finding bean in context



frankChan
Oct 31st, 2004, 10:02 AM
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




<bean id="someBeanDAO" class="com.test.dao.SomeBeanDaoImpl">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>

<bean id="someBeanManager" class="org.springframework.transaction.interceptor.Transa ctionProxyFactoryBean">
<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



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

Alef Arendsen
Oct 31st, 2004, 10:24 AM
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.py/springframework/spring/samples/jpetstore/war/WEB-INF/web.xml?rev=1.9&view=auto) to see how it works.



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

alesj
Oct 31st, 2004, 10:30 AM
Where do you have this <ref bean="packageManager"/> specified?

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

Rgds, Ales

frankChan
Oct 31st, 2004, 10:35 AM
Hi, I've included that in (prior to your message). It is being called because I see this in the logs.



2004-10-31 08&#58;30&#58;02,714 INFO &#91;org.springframework.beans.factory.xml.XmlBeanDefi n
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.XmlWebApp l
icationContext&#93; - <Bean factory for application context &#91;Root XmlWebApplicationC
ontext&#93;&#58; org.springframework.beans.factory.support.DefaultL istableBeanFactory de
fining beans &#91;dataSource,sessionFactory,transactionManager,some BeanDAO,someBeanManager&#93;; Root of BeanFactory hierarchy>
2004-10-31 08&#58;30&#58;03,975 INFO &#91;org.springframework.web.context.support.XmlWebApp l
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.

frankChan
Oct 31st, 2004, 10:36 AM
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"

frankChan
Oct 31st, 2004, 10:45 AM
My web.xml looks as follows:



<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.ContextLoaderServl et</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>

frankChan
Oct 31st, 2004, 10:58 AM
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.