I want to add an object to a servlet using Spring

I have two servlets in my application, and I want the class A object to be injected into both servlets, and I would also like the same ApplicationContext throughout the application, i.e. both servlets mentioned in the first answer of this question to SO: Spring servlet injection

Now I went through many questions like these, but I could not find something exactly that fits my question. To better explain, 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, here are two servlets now in applicationContext.xml. I want to pass an object to both of these servlets since, in accordance with the normal convention, I want this functionality:

 <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 don't know if this is possible or not, I'm new to spring, and I only have basic knowledge about Injection dependencies.

If anyone can help me, I would really really appreciate it. This would remove many of my doubts and help me move forward with Spring training.

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> 
+6
source share
2 answers

You are mixing two concepts: servlets and Spring ApplicationContext . Servlets are managed by your Servlet container, for example, take Tomcat. ApplicationContext managed by Spring.

When you declare a Servlet in the deployment descriptor as

 <servlet> <servlet-name>servletOne</servlet-name> <servlet-class>mypackage.servletOne</servlet-class> </servlet> <servlet-mapping> <servlet-name>servletOne</servlet-name> <url-pattern>/servletOne</url-pattern> </servlet-mapping> 

The Servlet container will instantiate your class mypackage.servletOne , register it, and use it to process requests. This is what it does with the DispatcherServlet , which is the foundation of Spring MVC.

Spring is an IoC container that uses ApplicationContext to control the number of beans. ContextLoaderListener loads the ApplicationContext root (from anywhere you speak of). DispatcherServlet uses this root context and must also load its own. The context must have the appropriate configuration for the DispatcherServlet to work.

A bean declaration in a Spring context, for example

 <bean id="servletFirst" class="mypackage.servletOne"> <property name="message" ref="classObject" /> </bean> 

, regardless of the fact that it is of the same type as the <servlet> declared in web.xml, it is not completely connected. The bean above has nothing to do with the <servlet> declaration in web.xml.

As in my answer here , since the ContextLoaderListener places the ApplicationContext , which it creates in ServletContext as an attribute, the ApplicationContext is available on any managed servlet container object. This way you can override HttpServlet#init(ServletConfig) in your custom HttpServlet classes, e.g.

 @Override public void init(ServletConfig config) throws ServletException { super.init(config); ApplicationContext ac = (ApplicationContext) config.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); this.someObject = (SomeBean)ac.getBean("someBeanRef"); } 

assuming your root ApplicationContext contains a bean called someBeanRef .

There are other alternatives to this. This, for example.

+14
source

If you want to use @Autowired or set a property through applicationContext.xml , then annotate your class using @Controller annotations

-2
source

All Articles