Javawebプロジェクト起動データ初期化
10080 ワード
1.需要:多くの場合、1つのwebプロジェクトが起動するとき、私たちは「多くのシステムパラメータを初期化し、例えばプロファイルを読み込む」か、データベーステーブルを初期化します.
2.解決方法:【サーブレットContextListener】インタフェース2.1を実現サーブレットContextListenerを実現したクラスを【web.xml】ファイルに配置する
2.2.独自の実装ロジックを追加
3.springboot使用時のデータ初期化
まずスキャン@Componentを設定します
次に初期化クラスを追加し、ApplicationContextAwareを実現
2.解決方法:【サーブレットContextListener】インタフェース2.1を実現サーブレットContextListenerを実現したクラスを【web.xml】ファイルに配置する
<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.jspwelcome-file>
welcome-file-list>
<listener>
<listener-class>com.chinaso.init.StartInitlistener-class>
listener>
<filter>
<filter-name>struts2filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
filter-class>
filter>
<filter-mapping>
<filter-name>struts2filter-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 getBean(Class clazz){
return getApplicationContext().getBean(clazz);
}
// name, Clazz Bean
public static T getBean(String name,Class clazz){
return getApplicationContext().getBean(name, clazz);
}
}
public class QuartzJobManager {
public static QuartzJobManager I = new QuartzJobManager();
public QuartzJobManager() {
}
public void init() {
// do something...
}
}