PDA

View Full Version : Can't inject anything into servlet



ljubisap
Jul 24th, 2009, 07:52 AM
Hello

I can't inject any other spring object into Servlet. I'm using servlet to upload image from Flex into database. Spring-Flex integration doesn't uses ContextLoaderListener. I don't have WebApplicationContext in my servlet any more, so can some one please explain to me what would be the easiest way to inject some Spring bean into servlet.
Here is the listing of my web.xml related to this integration:


<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/applicationContext/applicationContext-*.xml
</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j/log4j.xml
</param-value>
</context-param>


<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>

<listener>
<listener-class>
org.springframework.web.context.request.RequestCon textListener
</listener-class>
</listener>

<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext/applicationContext-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>


I tried even to annotate servlet with @Configurable, @Service annotations but for some reason @Autowired don't work inside. There are no error, nothing, I just see that my service is null, so when I try to call it I get NullPointerException.


When I try to configure ContextLoaderListener and to remove RequestContextListener I get this exception:


org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name '_messageBrokerDefaultHandlerMapping': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name '_messageBroker': Invocation of init method failed; nested exception is java.lang.RuntimeException: MessageBroker already defined from MessageBrokerServlet with init parameter messageBrokerId = '_messageBroker'
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:480)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory$1.run(AbstractAutowireC apableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 64)
at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:222)
at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:261 )
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.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:728)
at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:380)
at org.springframework.web.servlet.FrameworkServlet.c reateWebApplicationContext(FrameworkServlet.java:4 02)
at org.springframework.web.servlet.FrameworkServlet.i nitWebApplicationContext(FrameworkServlet.java:316 )
at org.springframework.web.servlet.FrameworkServlet.i nitServletBean(FrameworkServlet.java:282)
at org.springframework.web.servlet.HttpServletBean.in it(HttpServletBean.java:126)
at javax.servlet.GenericServlet.init(GenericServlet.j ava:212)
at org.apache.catalina.core.StandardWrapper.loadServl et(StandardWrapper.java:1161)
at org.apache.catalina.core.StandardWrapper.load(Stan dardWrapper.java:981)
at org.apache.catalina.core.StandardContext.loadOnSta rtup(StandardContext.java:4058)
at org.apache.catalina.core.StandardContext.start(Sta ndardContext.java:4364)
at org.apache.catalina.core.ContainerBase.addChildInt ernal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(Co ntainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(Sta ndardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployDirec tory(HostConfig.java:924)
at org.apache.catalina.startup.HostConfig.deployDirec tories(HostConfig.java:887)
at org.apache.catalina.startup.HostConfig.deployApps( HostConfig.java:492)
at org.apache.catalina.startup.HostConfig.start(HostC onfig.java:1147)
at org.apache.catalina.startup.HostConfig.lifecycleEv ent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLife cycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(Standa rdHost.java:719)
at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(Stan dardEngine.java:443)
at org.apache.catalina.core.StandardService.start(Sta ndardService.java:516)
at org.apache.catalina.core.StandardServer.start(Stan dardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalin a.java:578)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootst rap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstr ap.java:413)
Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name '_messageBroker': Invocation of init method failed; nested exception is java.lang.RuntimeException:

I see what the problem is, but I don't see where this context is loaded twice.

Any help is appreciated.
Thanks.

Ljubisa

jeremyg484
Jul 24th, 2009, 11:30 AM
Take a look at http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/servlet/HttpServletBean.html

You will need to have a root context loaded by the ContextLoaderListener in order for it to work correctly. You should not be loading the same config files in both the ContextLoaderListener and DispatcherServlet. The DispatcherServlet's context extends the root context already loaded by the ContextLoaderListener. Either load everything from the ContextLoaderListener, or segregate things in a similar manner to how it's done in the testdrive sample app.

You cannot inject a Servlet with @Service, @Autowired, etc., because Spring does not control its lifecycle. @Configurable can be made to work, but using the HttpServletBean is much simpler as there are no requirements to use AspectJ weaving as there is with @Configurable.

ljubisap
Jul 24th, 2009, 12:34 PM
Hello Jeremy,

Thank you very much for your fast response. I've already fixed it by removing init parameter from DispatchServlet. Thanks for the brief explanation.

Regards,
Ljubisa