1.WebApplicationContext的研究
<listener>
</listener>
或者用ContextLoaderServlert亦可(加<load-on-startup>1</load-on-startup>)
2.ServletContext详解
存取共享数据方法:
getAttribute(Stringname)
这里得到了spring 的webApplicationContext,spring的bean都放在里面,然后直接getBean就可以得到了
通过WebApplicationContextUti
只需要传入application对象即可.
如果是struts2中通过如下方式获取application对象:
- ServletContext
application = ServletActionContext.getRequest().getSession().getServletContext(); -
以下是获取applicationContext对象;
- ApplicationContext
context = WebApplicationContextUti ls.getWebApplicationContext (application); - UserDao
bd = (UserDao)context.getBean(spring中配置bean ID的名称);
/**
*
*/
private static final long serialVersionUID = 1L;
private Log log = LogFactory.getLog(SiteContextLoaderServlet.class);
public static EmailService emailService;
@Override
public void init(ServletConfig config) throws ServletException {
ServletContext servletContext = config.getServletContext();
WebApplicationContext context = WebApplicationContextUtils
.getWebApplicationContext(servletContext);
ApplicationContextUtil.setApplicationContext(context);
SiteContext.getInstance().contextRealPath = config.getServletContext()
.getRealPath("/");
log.info("SiteContext.getInstance().contextRealPath: "+SiteContext.getInstance().contextRealPath);
// set constants value to app context
}
}
servletContext.setAttribute("appConstant", new AppConstant());