0

I've created a Servlet and added configuration in web.xml:

<servlet>
     <servlet-name>MyServlet</servlet-name>
     <servlet-class>com.foo.web.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/myservlet/*</url-pattern>
</servlet-mapping>

Registered a bean for it:

<bean id="MyServlet" class="com.foo.MyServlet" autowire="byType">
    <property name="MyService" ref="MyManager"/>
</bean>

I've created a bean which is injected into MyServlet

<bean id="MyManager" class="com.foo.MyService" autowire="byType">
</bean>

There s a setter method in the servlet used by spring for setting the property to the bean:

 public class MyServlet extends HttpServlet {

       private static final long serialVersionUID = 1L;

       private MyManager myService;
    
       @Override
       protected void doGet(HttpServletRequest req, HttpServletResponse response) throws 
          ServletException, IOException {
        
      }
    
       public void setMyService(MyManager myManager){
          this.myService = myManager;
       }
    
}

Autowiring is done correctly during the application startup, but when Servlet "catches" some request, this property (myService) is not instantiated (it's null). How or can I even have a Servlet with some autwired properties?

wdc
  • 2,623
  • 1
  • 28
  • 41
  • 1
    What do you mean by _registered a bean for it in `web.xml`_? `web.xml` is a Servlet container configuration, not a Spring (bean) context configuration. – Sotirios Delimanolis Jan 27 '22 at 15:48
  • @SotiriosDelimanolis Sorry, my bad, it was not web.xml, it was in valid xml spring configuration. – wdc Jan 27 '22 at 16:17
  • Your Servlet container won't use your custom Servlet if it's not defined in its configuration. Are you also registering the servlet in web.xml? Or do you have that `@WebServlet`annotation on your servlet class? – Sotirios Delimanolis Jan 27 '22 at 16:21
  • @SotiriosDelimanolis it is defined in `web.xml`. I've updated the question. It catches the requests as expected, it's just that this dependency is not being injected when the Servlet catches the request. It's like that Spring creates a new `Servlet` object, and doesn't load it from the context. During the app startup, I can see that setter method is called correctly and the attribute is set to the Servlet. We don't have annotations on this project, so only `web.xml`. – wdc Jan 27 '22 at 16:27
  • 2
    You have two different `MyServlet` instances. One is created by the Servlet container because of the web.xml declaration. The other is created by Spring because of the bean definition. Your Servlet container is using its instance, not Spring's. – Sotirios Delimanolis Jan 27 '22 at 16:32
  • @SotiriosDelimanolis Makes sense. Thank you for the explanation. Any idea how to autowire the dependency into the instance created by Servlet Container? I think I saw people do that before. – wdc Jan 27 '22 at 16:37
  • 1
    There's some old school stuff like [this](https://stackoverflow.com/questions/11843690/autowiring-in-servlet) or [this](https://stackoverflow.com/questions/23839226/autowire-null-inside-a-servlet/23839705). But I think Spring now has an adapter for this kind of thing but I can't find it. – Sotirios Delimanolis Jan 27 '22 at 16:56
  • If you need injection, use CDI in a container supporting it. – Thorbjørn Ravn Andersen Jan 27 '22 at 22:08

0 Answers0