在servlet中如何使用被Spring管理的service
我的使用場景是SpringMvc+MyBatis,我總結(jié)了以下兩種方式,三種方法。兩種方式指的是采用注入方式和獲取spring管理的bean。三種方法指的是,代理注入、硬編碼獲取bean和實(shí)現(xiàn)ApplicationContextAware接口獲取bean。
第一種方式:采用注入方式。
編寫一個(gè)代理類,代碼如下:
@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,代碼如下:
@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("執(zhí)行取緩存操作: " + name);
}else if("f".equals(flag)){
//放緩存
String username = request.getParameter("username");
globalCacheService.deleteCacheValue("_name1");
if(!StringUtil.isBlank(username)){
System.out.println("執(zhí)行存緩存操作: " + username);
globalCacheService.setCacheValue("_name1", username, 28800);
}else{
System.out.println("執(zhí)行存緩存操作: " + 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中配置如下:
memcacheServlet
com.hsis.core.servlet.web.ProxyServlet
memcacheServlet
*.to
第二種方式:獲取spring管理的service,采用硬編碼或者實(shí)現(xiàn)AplicationContextAware接口。
2.1 采用硬編碼方法如下:
ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
或者
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
LzxService lzxService = (LzxService)wac.getBean("lzxService");
注:WebApplicationContext繼承的ApplicationContext。
SpringContextUtil,代碼如下:
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 getBean(String name, Class requiredClass){
return applicationContext.getBean(name, requiredClass);
}
}
applicationContext.xml中配置一下:
在servlet中使用即可:
globalCacheService = (GlobalCacheService) SpringContextUtil.getBean("globalCacheService", GlobalCacheService.class);
實(shí)現(xiàn)Aware接口的類,初始化之后可以獲取對(duì)應(yīng)的資源,實(shí)現(xiàn)ApplicationContextAware接口的bean,初始化后被注入applicationContext實(shí)例。
如果使用ClassPathXmlApplicationContext、FileSystemClassPathXmlApplicationContext和FileSystemXmlApplicationContext等對(duì)象去加載Spring配置文件,會(huì)生成一個(gè)新的application對(duì)象,這樣會(huì)產(chǎn)生冗余。
本文僅代表作者觀點(diǎn),版權(quán)歸原創(chuàng)者所有,如需轉(zhuǎn)載請?jiān)谖闹凶⒚鱽碓醇白髡呙帧?/p>
免責(zé)聲明:本文系轉(zhuǎn)載編輯文章,僅作分享之用。如分享內(nèi)容、圖片侵犯到您的版權(quán)或非授權(quán)發(fā)布,請及時(shí)與我們聯(lián)系進(jìn)行審核處理或刪除,您可以發(fā)送材料至郵箱:service@tojoy.com






