Results 1 to 6 of 6

Thread: @Autowire in a servlet

  1. #1
    Join Date
    Oct 2009
    Posts
    6

    Default @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. #2
    Join Date
    Nov 2006
    Location
    Columbus, OH
    Posts
    143

    Default

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


    --- scratch that, I misread your post.
    Regards,

    Joshua Preston

    --

    "The Guide says that there is an art to flying," said Ford, "or rather a knack. The knack lies in learning how to throw yourself at the ground and miss."

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4
    Join Date
    Oct 2009
    Posts
    6

    Default

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

  5. #5
    Join Date
    Nov 2006
    Location
    Boston, MA
    Posts
    303

    Default

    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

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •