1. 程式人生 > >java web啟動後執行初始化任務

java web啟動後執行初始化任務

寫一個類繼承ApplicationListener,可以直接引用下述程式碼,然後呼叫相應的方法。

package com.linewell.system;

import com.linewell.cache.ApplicationData;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Component
public class InitializationProcessor implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { if (contextRefreshedEvent.getApplicationContext().getParent() == null) { System.out.println(
"run onApplicationEvent method");
//dosomething } } }