请教在Spring MVC中,如何实现启动时从数据库读取信息
1个回答
展开全部
//给个例子你看下
package com.bing.personalweb.filter;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bing.personalweb.business.errormsg.service.ErrorMsgService;
import com.bing.personalweb.business.user.service.UserService;
import com.bing.personalweb.testpackage.Consumer;
import com.bing.personalweb.testpackage.QueueController;
public class startFilter implements Filter{
private final static Logger logger = Logger.getLogger(startFilter.class);
public void init(FilterConfig fc) throws ServletException {
new Thread(new Runnable() {
@Override
public void run() {
//加载错误信息
logger.info("开始加载 ErrorMsg--------------------");
loadErrorMsg();
logger.info("加载 ErrorMsg 完成--------------------");
}
}).start();
}
public void destroy() {
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
}
@SuppressWarnings("resource")
public void loadErrorMsg(){
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring-mybatis.xml");
ErrorMsgService errorMsgService = (ErrorMsgService) ac.getBean("errorMsgService");
errorMsgService.loadErrorMsgMap();
}
}
web.xml中:
<filter>
<filter-name>startFilter</filter-name>
<filter-class>com.bing.personalweb.filter.startFilter</filter-class>
</filter>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询