系统加载时执行的类,也是我们将需要加载的参数配置的地方 。必须实现 ServletContextListener
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.chain.store.bean.SystemParamBean;
import com.chain.store.service.impl.SystemParamServicesImpl;
public class LoadConfiguration implements ServletContextListener {
private static final long serialVersionUID = 1L;
//private SystemParamServicesImpl sysParamService;
private static WebApplicationContext springContext;
private SystemParamBean sysBean ;
private ServletContext context;
public LoadConfiguration(){
super();
}
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("系统停止......");
}
@SuppressWarnings("unchecked")
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
System.out.println("系统启动加载......");
springContext = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
SystemParamServicesImpl sysServices = (SystemParamServicesImpl)springContext.getBean("sysParamService");
sysBean = sysServices.listAllParams();
context = sce.getServletContext();
context.setAttribute("sysParams", sysBean);
}
public static ApplicationContext getApplicationContext() {
return springContext;
}
}
说明 :
SystemParamBean 放置我需要加载的系统参数
sysParamService 是我在 applicationContext.xml 中配置的从数据库中得到参数值的bean
当然不要忘记在 web.xml 中添加
在Action 中得到系统参数:
System.out.println("----------系统参数----------");
System.out.println("NEWS_SHOWIMG " + sysBean.getNEWS_SHOWIMG());
System.out.println("---------------------------");
参考文章 :
http://hi.baidu.com/29163077/blog/item/b3b77bdd5c1a94ea76c638f7.html