1.需求:很多时候,在一个 web 项目启动的时候,我们都要【初始化很多系统参数,比如读取配置文件】,或者初始化数据库表…
2.解决方法:实现【 ServletContextListener】 接口
2.1 把实现了ServletContextListener 的类配置到【 web.xml】 文件中
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>com.chinaso.init.StartInit</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2.2.加入自己的实现逻辑
public class StartInit implements ServletContextListener {
static final Logger logger = LoggerFactory.getLogger(StartInit.class);
// 系统初始化执行方法
public void contextDestroyed(ServletContextEvent e) {
logger.info("系统停止...");
}
public void contextInitialized(ServletContextEvent e) {
logger.info("系统初始化开始...");
// 获取项目根目录
String root_path = e.getServletContext().getRealPath("/");
logger.info("application path : {}",root_path);
// 初始化 ConfigFactorty
ConfigFactory.init(root_path);
// 初始化数据链接信息
DBManager.init();
// 初始化定时统计任务
TaskManager.init();
// 初始化用户信息查询位置
UserInfo.init();
logger.info("系统初始化结束...");
}
}
3.使用springboot时的数据初始化
首先要配置扫描@Component
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* 启动类
*
*/
@SpringBootApplication
@EnableScheduling //@EnableScheduling (执行定时器任务) @Scheduled的任务并后台执行。
@ServletComponentScan //扫描过(执行过滤器)滤器 注解
//@ComponentScan(basePackages={"util}) // 扫描该包路径下的所有spring组件
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
builder.sources(this.getClass());
return super.configure(builder);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
其次添加初始化类,实现ApplicationContextAware
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import stock.simulator.trader.web.quartz.QuartzJobManager;
/**
* Created by dzd-technology01 on 2017/9/25.
*/
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextUtil.applicationContext = applicationContext;
// ***---初始化操作---***
QuartzJobManager.I.init();
}
public static Object getBean(String beanName){
return applicationContext.getBean(beanName);
}
//通过class获取Bean.
public static <T> T getBean(Class<T> clazz){
return getApplicationContext().getBean(clazz);
}
//通过name,以及Clazz返回指定的Bean
public static <T> T getBean(String name,Class<T> clazz){
return getApplicationContext().getBean(name, clazz);
}
}
public class QuartzJobManager {
public static QuartzJobManager I = new QuartzJobManager();
public QuartzJobManager() {
}
public void init() {
// do something...
}
}