日期:2014-05-18 浏览次数:20780 次
public class SpringLoaderListener extends ContextLoaderListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
super.contextInitialized(event);
ServletContext t1 = event.getServletContext();
WebApplicationContext t2 = WebApplicationContextUtils.getWebApplicationContext(t1);
SpringContextUtil.setApplicationContextStaticlly(t2);
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
}
package com.woods.common.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.WebApplicationContext;
public class SpringContextUtil implements ApplicationContextAware {
// Spring应用上下文环境
private static WebApplicationContext applicationContext;
public static void setApplicationContextStaticlly(WebApplicationContext vApplicationContext)
{
SpringContextUtil.applicationContext = vApplicationContext;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextUtil.applicationContext = (WebApplicationContext) applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 获取对象
*/
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
}
<context-param>
<param-name>contextConfigLocati