Announcement Announcement Module
Collapse
No announcement yet.
Can't inject anything into servlet Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can't inject anything into servlet

    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:
    Code:
    	<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.RequestContextListener
    		</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:
    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_messageBrokerDefaultHandlerMapping': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: 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.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
    	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:402)
    	at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:316)
    	at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:282)
    	at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:126)
    	at javax.servlet.GenericServlet.init(GenericServlet.java:212)
    	at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)
    	at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:981)
    	at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4058)
    	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4364)
    	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
    	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
    	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
    	at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:924)
    	at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:887)
    	at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
    	at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1147)
    	at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
    	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    	at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    	at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    	at org.apache.catalina.core.StandardService.start(StandardService.java:516)
    	at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    	at org.apache.catalina.startup.Catalina.start(Catalina.java:578)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
    Caused by: org.springframework.beans.factory.BeanCreationException: 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
    Last edited by ljubisap; Jul 24th, 2009, 08:14 AM.

  • #2
    Take a look at http://static.springsource.org/sprin...rvletBean.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.

    Comment


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

      Comment

      Working...
      X