Javaweb 專案啟動資料初始化
阿新 • • 發佈:2019-02-12
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...
}
}