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: Spring injection Into Servlet
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:
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 {
}
}
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:
<bean id="servletFirst" class="mypackage.servletOne">
<property name="message" ref="classObject" />
</bean>
<bean id="servletFirst" class="mypackage.servletTwo">
<property name="message" ref="classObject" />
</bean>
<bean id="classObject" class="mypackage.classA">
</bean>
I dont know if this is possible or not, I am new to spring and I have only basic knowledge of dependency Injection.
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.
This is web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>servletOne</servlet-name>
<servlet-class>mypackage.servletOne</servlet-class>
</servlet>
<servlet>
<servlet-name>servletTwo</servlet-name>
<servlet-class>mypackage.servletTwo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servletOne</servlet-name>
<url-pattern>/servletOne</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servletTwo</servlet-name>
<url-pattern>/servletTwo</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
300
</session-timeout>
</session-config>
</web-app>