Announcement Announcement Module
Collapse
No announcement yet.
@Autowire in a servlet Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Autowire in a servlet

    Hi,

    I guess, my question is pretty simple... Is there are way to use @Autowire for a component declared with @Service inside a servlet?

    Service implementation example:
    Code:
    @Service
    public class MyService implements Service
    {
        public String test() { return "test"; }
    }
    Servlet example:
    Code:
    public class MyServlet extends HttpServlet
    {
        @Autowired
        private Service service;
    
        public void doGet(.........)
        {
            System.out.print(service.test());
        }
    }
    Since servlets have their own lifecycle of instantiation, initialization, etc., which is certainly different from Spring, it seems that this would not be possible...

    However, perhaps, I am overlooking something here and maybe Spring can plug into the servlet lifecycle to make sure the @Autowired actually works.

    I've tried the example pretty much as is and the 'service' is null. (I did use the Spring-provided ContextLoaderListener and configured it in web.xml).

    Any suggestions?

    Thanks,
    Yaakov.

  • #2
    I assume you have a setter for Service defined in your servlet?


    --- scratch that, I misread your post.

    Comment


    • #3
      about the fifth time today that I answer this question.

      Spring can only inject beans into other beans it knows about. So your Servlet/Filter/Listener is not going to be injected because that is outside the scope and control of spring. (although for filters there is a workaround).

      In general simply retrieve the ApplicationContext and lookup the dependencies.

      Comment


      • #4
        Originally posted by Marten Deinum View Post
        Spring can only inject beans into other beans it knows about. So your Servlet/Filter/Listener is not going to be injected because that is outside the scope and control of spring. (although for filters there is a workaround).
        Right. That's what I thought (and stated in the question)... It's the "workaround" I was wondering about. Apparently, it doesn't exist.

        Thanks for answering. (for the 5th time. )

        Yaakov.

        Comment


        • #5
          What you can do, however, is export a spring-managed bean (e.g. your service) into the servlet context as a context attribute - using a ServletContextAttributeExporter definition in you application context XML. For example:

          Code:
          <bean id="myService" class="..."/>
          
          <bean class="org.springframework.web.context.support.ServletContextAttributeExporter">
             <property name="attributes">
                <map>
                   <entry key="myServiceAttName"><ref bean="myService"/>
                </map>
             </property>
          </bean>
          Your servlet does not have to be Spring-aware (no need to make it dependent on WebApplicationContext) and will be able to extract that attribute from the ServletContext object simply by using getAttribute("myServiceAttName"). Naturally, the service would have to expect an attribute with the name "myServiceAttName" in the servlet context.
          HTH

          Comment


          • #6
            That is one workaround . You can also configure your servlet in spring and use a ServletWrappingController and use a DispatcherServlet. That way you could inject your controller but you remove it from the web.xml.

            Comment

            Working...
            X