View Full Version : *-servlet.xml reference application.xml for SpringMVC
difranr
Feb 12th, 2006, 02:22 PM
how do I reference my application.xml in *-servlet.xml so that I can reference my beans defined in it?
Colin Yates
Feb 12th, 2006, 02:41 PM
You can use the "<import resource=...>", but if you are referring to the applicationContext.xml; it should already be included by default.
difranr
Feb 12th, 2006, 03:53 PM
Maybe the import resource would work, for some reason it did not get included by default as it through an error saying it could not find the resource from my application.xml.
dhewitt
Feb 12th, 2006, 05:35 PM
Do you have a ContextLoaderListener or ContextLoaderServlet defined in your web.xml? If so, that should take care of automatically starting your root context from WEB-INF/applicationContext.xml. Then, any dispatcher servlets will be defined as children of this context, and will be able to reference beans from it.
Also, check the name of the file you are using, as the default is applicationContext.xml, not application.xml.
difranr
Feb 12th, 2006, 07:07 PM
I have context loader servlet; from web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/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>
Now, I wonder if the issue is that I have it in a seperate directory [WEB-INF/spring] versus have it directly in /WEB-INF like my *-servlet?
difranr
Feb 12th, 2006, 08:22 PM
Nope, moving applicationContext.xml into WEB-INF did not help. Any other thoughts out there?
Colin Yates
Feb 13th, 2006, 04:41 AM
Nope, moving applicationContext.xml into WEB-INF did not help. Any other thoughts out there?
Try removing the contextParam entirely.
So to be clear; you have /WEB-INF/applicationContext.xml which contains <bean id="beanA"..> and yourServlet-servlet.xml that references beanA....
How do you know that beanA cannot be referenced by -servlet.xml? NPE?
difranr
Feb 13th, 2006, 10:45 AM
Yes, I removed the the context-param and yes I had beanA in my applicationContext.xml and in my *-servlet.xml I had some like [I am nto right in front of my code]:
<bean id="myServletBean" class="abc.Dummy">
<property name="target" ref="myDBDAO"/>
</bean>
Where myDBDAO is defined in my applicationContext.xml
Colin Yates
Feb 13th, 2006, 10:46 AM
Can you post both files please.
Also, the exception stack that alerted you to the fact it was null.
difranr
Feb 13th, 2006, 08:31 PM
Per your request, here are all the relevant files and the exception:
20:20:46,046 ERROR [DispatcherServlet] Context initialization failed
org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'userTasksReportController' defined in ServletContext resource [/WEB-INF/springapp-servlet.xml]: Can't resolve reference to bean 'taskProviderService' while setting property 'taskProvider'; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'taskProviderService' is defined: org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans [userTasksReportController,contactTasks,urlMapping, viewResolver]; root of BeanFactory hierarchy
org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'taskProviderService' is defined: org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans [userTasksReportController,contactTasks,urlMapping, viewResolver]; root of BeanFactory hierarchy
at org.springframework.beans.factory.support.DefaultL istableBeanFactory.getBeanDefinition(DefaultListab leBeanFactory.java:349)
at org.springframework.beans.factory.support.Abstract BeanFactory.getMergedBeanDefinition(AbstractBeanFa ctory.java:671)
at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:198)
at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:147)
at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveReference(BeanDefinitio nValueResolver.java:176)
at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveValueIfNecessary(BeanDe finitionValueResolver.java:105)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyPropertyValues(Abs tractAutowireCapableBeanFactory.java:1012)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:823)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:345)
at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:226)
at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:147)
at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:275)
at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:318)
at org.springframework.web.context.support.AbstractRe freshableWebApplicationContext.refresh(AbstractRef reshableWebApplicationContext.java:134)
at org.springframework.web.servlet.FrameworkServlet.c reateWebApplicationContext(FrameworkServlet.java:3 05)
at org.springframework.web.servlet.FrameworkServlet.i nitWebApplicationContext(FrameworkServlet.java:250 )
at org.springframework.web.servlet.FrameworkServlet.i nitServletBean(FrameworkServlet.java:219)
at org.springframework.web.servlet.HttpServletBean.in it(HttpServletBean.java:112)
at javax.servlet.GenericServlet.init(GenericServlet.j ava:211)
at org.apache.catalina.core.StandardWrapper.loadServl et(StandardWrapper.java:1029)
at org.apache.catalina.core.StandardWrapper.load(Stan dardWrapper.java:862)
at org.apache.catalina.core.StandardContext.loadOnSta rtup(StandardContext.java:4013)
dgynn
Feb 13th, 2006, 11:33 PM
Try setting the load-on-startup value to 2 for the 'springapp' servlet so you can be sure that the ContextLoaderServlet executes first.
difranr
Feb 14th, 2006, 07:18 AM
That worked! Thanks!
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.