方法一:
ContextLoader ctl=new ContextLoader();
ctl.initWebApplicationContext(ServletContext);
此方法需要在web.xml文件中配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application.xml</param-value>
<context-param>
方法二:
XmlWebApplicationContext ctx=new XmlWebApplicationContext();
ctx.setConfigLocations(new String[]{"/WEB-INF/config/application.xml"});
ctx.setServletContext(ServletContext);
ctx.refresh();
appContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ctx);