Spring DI in servlet

Today, I'll show you how can inject a bean into your servlet.

1. Using ServletContextListener

The ServletContextListener will enable your servlets to access the Spring ApplicationContext.

package com.vedrax.servlet;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class SpringApplicationContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
//we get the Spring application context
ApplicationContext applicationContext = WebApplicationContextUtils
.getWebApplicationContext(servletContextEvent.getServletContext());
//make accessible the application context via an attribute
servletContextEvent.getServletContext()
.setAttribute("applicationContext", applicationContext);
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}


In your servlet :

package com.vedrax.servlet;
import com.vedrax.core.clientproduct.ExpiredClientProductVO;
import com.vedrax.service.ClientProductService;
import org.springframework.context.ApplicationContext;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet(
name = "SurveyScheduler",
description = "Create survey automatically",
urlPatterns = "/admin/survey-scheduler"
)
public class SurveyScheduler extends HttpServlet {
private ClientProductService clientProductService;
public void init(ServletConfig config) throws ServletException {
super.init(config);
ApplicationContext applicationContext = (ApplicationContext) config
.getServletContext()
.getAttribute("applicationContext");
this.clientProductService = applicationContext.getBean("ClientProductService", ClientProductService.class);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1. get expired client products (next month)
List<ExpiredClientProductVO> expiredClientProducts = clientProductService.findExpiredProducts();
}
}
view raw SurveyScheduler hosted with ❤ by GitHub


2. Using the SpringBeanAutowiringSupport

I personally prefer the following solution :

package com.vedrax.servlet;
import com.vedrax.core.clientproduct.ExpiredClientProductVO;
import com.vedrax.service.ClientProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet(
name = "SurveyScheduler",
description = "Create survey automatically",
urlPatterns = "/admin/survey-scheduler"
)
public class SurveyScheduler extends HttpServlet {
@Autowired
private ClientProductService clientProductService;
public void init(ServletConfig config) throws ServletException {
super.init(config);
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
config.getServletContext());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1. get expired client products (next month)
List<ExpiredClientProductVO> expiredClientProducts = clientProductService.findExpiredProducts();
}
}


thanks for sharing...

Comments

Popular posts from this blog

Spring JPA : Using Specification with Projection

Chip input using Reactive Form