Spring DI in servlet
Today, I'll show you how can inject a bean into your servlet.
thanks for sharing...
1. Using ServletContextListener
The
In your servlet :
ServletContextListener
will enable your servlets to access the Spring ApplicationContext
.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
2. Using the SpringBeanAutowiringSupport
I personally prefer the following solution :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Post a Comment