在servlet中如何使用被Spring管理的service
首先真的很感謝這篇文章的作者,我在這次的專案中遇到了這個問題,看了很多網上的資料都沒解決,直到看到這篇文章才解決,我用的是方法2.2。
剛開始我把service設定成servlet的成員變數,不知道為什麼不行,這種方式讓這個servlet都無法使用,希望知道原因的朋友能告知。以至於我以為這篇文章也是不行,感到灰心喪氣,兩天都沒解決這個問題。剛剛我再次嘗試了一次,把service放到方法裡,設定成區域性變數,沒想到竟然成功初始化了,而且成功呼叫後臺取到值返回,讓我差點“喜極而泣”,真的很開心,明天就可以接著把專案往下做了,具體內容參照以下。再次感謝作者!
-------------分割線------------------------
我的使用場景是SpringMvc+MyBatis,我總結了以下兩種方式,三種方法。兩種方式指的是採用注入方式和獲取spring管理的bean。三種方法指的是,代理注入、硬編碼獲取bean和實現ApplicationContextAware介面獲取bean。
第一種方式:採用注入方式。
編寫一個代理類,程式碼如下:
Java程式碼- @SuppressWarnings("serial")
- public class ProxyServlet extends HttpServlet {
- @Override
-
public void service(ServletRequest req, ServletResponse res)
- throws ServletException, IOException {
- proxyServlet.service(req, res);
- }
- @Override
- public void init() throws ServletException {
- this.targetBean = getServletName();
- getServletBean();
- proxyServlet.init(getServletConfig());
-
}
- private String targetBean;
- private Servlet proxyServlet;
- private void getServletBean(){
- WebApplicationContext wac = WebApplicationContextUtils
- .getRequiredWebApplicationContext(getServletContext());
- this.proxyServlet = (Servlet) wac.getBean(targetBean);
- }
- }
然後編寫需要注入service的servlet,程式碼如下:
Java程式碼- @Component
- public class MemcacheServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- @Autowired
- private GlobalCacheService globalCacheService;
- /**
- * @see HttpServlet#HttpServlet()
- */
- public MemcacheServlet() {
- super();
- }
- /**
- * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- String flag = request.getParameter("flag");
- globalCacheService.test();
- if("q".equals(flag)){
- //取快取
- String name = (String)globalCacheService.getCacheValue("_name1", Object.class);
- System.out.println("執行取快取操作: " + name);
- }else if("f".equals(flag)){
- //放快取
- String username = request.getParameter("username");
- globalCacheService.deleteCacheValue("_name1");
- if(!StringUtil.isBlank(username)){
- System.out.println("執行存快取操作: " + username);
- globalCacheService.setCacheValue("_name1", username, 28800);
- }else{
- System.out.println("執行存快取操作: " + username);
- globalCacheService.setCacheValue("_name1", "lzx", 28800);
- }
- }
- }
- /**
- * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- response.setContentType("text/html");
- this.doGet(request, response);
- }
- }
最後在web.xml中配置如下:
Xml程式碼- <servlet>
- <servlet-name>memcacheServlet</servlet-name>
- <servlet-class>com.hsis.core.servlet.web.ProxyServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>memcacheServlet</servlet-name>
- <url-pattern>*.to</url-pattern>
- </servlet-mapping>
第二種方式:獲取spring管理的service,採用硬編碼或者實現AplicationContextAware介面。
2.1 採用硬編碼方法如下:
Java程式碼- ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
- 或者
- WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
- LzxService lzxService = (LzxService)wac.getBean("lzxService");
注:WebApplicationContext繼承的ApplicationContext。
2.2 採用實現AplicationContextAware介面方法,首先建立一個類SpringContextUtil,程式碼如下:
Java程式碼- import org.springframework.beans.BeansException;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationContextAware;
- public class SpringContextUtil implements ApplicationContextAware {
- private static ApplicationContext applicationContext;
- @Override
- public void setApplicationContext(ApplicationContext applicationContext)
- throws BeansException {
- applicationContext = applicationContext;
- }
- public static ApplicationContext getApplicationContext(){
- return applicationContext;
- }
- public static Object getBean(String name){
- return applicationContext.getBean(name);
- }
- public static <T> T getBean(String name, Class<T> requiredClass){
- return applicationContext.getBean(name, requiredClass);
- }
- }
applicationContext.xml中配置一下:
Java程式碼- <bean class=”SpringContextUtil” />
在servlet中使用即可:
Java程式碼- globalCacheService = (GlobalCacheService) SpringContextUtil.getBean("globalCacheService", GlobalCacheService.class);
注:實現Aware介面的類,初始化之後可以獲取對應的資源,實現ApplicationContextAware介面的bean,初始化後被注入applicationContext例項。
如果使用ClassPathXmlApplicationContext、FileSystemClassPathXmlApplicationContext和FileSystemXmlApplicationContext等物件去載入Spring配置檔案,會生成一個新的application物件,這樣會產生冗餘。