title: I want to inject an object in servlet using Spring
body: <p>I have a two servlets in my application and I want an object of class A to be injected to both the servlets, and I would also like the same ApplicationContext throughout the application i.e. both servlets as mentioned in the first answer of this question on SO:
  <a href="https://stackoverflow.com/questions/18745770/spring-injection-into-servlet">Spring injection Into Servlet</a></p>
  
  <p>Now I went through many questions like these but couldnt find something exactly that matches my question. To explain better Ill write a rough code here:</p>
  
  <pre><code>public class servletOne extends HttpServlet {
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
  
      }
  }
  
  public class servletTwo extends HttpServlet {
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
  
      }
  }
  </code></pre>
  
  <p>So above are the two servelts now in applicationContext.xml I want to pass an object to both these servlets so as per normal convention I want a functionality like this:</p>
  
  <pre><code>&lt;bean id="servletFirst" class="mypackage.servletOne"&gt;
          &lt;property name="message" ref="classObject" /&gt;
  
  
  &lt;/bean&gt;
  &lt;bean id="servletFirst" class="mypackage.servletTwo"&gt;
          &lt;property name="message" ref="classObject" /&gt;
  
  
  &lt;/bean&gt;
  
  &lt;bean id="classObject" class="mypackage.classA"&gt;
  
      &lt;/bean&gt;
  </code></pre>
  
  <p>I dont know if this is possible or not, I am new to spring and I have only basic knowledge of dependency Injection.</p>
  
  <p>If anyone can help me with this I would really really appreciate it. This would clear a lot of my doubts and help me move forward in the process of learning Spring. </p>
  
  <p>This is web.xml</p>
  
  <pre><code>    &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;2&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;servletOne&lt;/servlet-name&gt;
          &lt;servlet-class&gt;mypackage.servletOne&lt;/servlet-class&gt;
      &lt;/servlet&gt;
  &lt;servlet&gt;
          &lt;servlet-name&gt;servletTwo&lt;/servlet-name&gt;
          &lt;servlet-class&gt;mypackage.servletTwo&lt;/servlet-class&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.htm&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;servletOne&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/servletOne&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;servletTwo&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/servletTwo&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;session-config&gt;
          &lt;session-timeout&gt;
              300
          &lt;/session-timeout&gt;
      &lt;/session-config&gt;
  &lt;/web-app&gt;
  </code></pre>