1.Spring项目启动时,加载相关初始化配置

时间:2023-03-09 18:22:37
1.Spring项目启动时,加载相关初始化配置

Spring项目启动时,会加载一些常用的配置:

1、加载spring上下文

SpringApplicationContextUtils.initApplicationContext(event.getServletContext());

2、加载属性文件

EsbCommsUtils.initComms(event.getServletContext());
 public class EsbCommsUtils {

     private static Log logger = LogFactory.getLog(EsbCommsUtils.class);

     public static final Properties properties = new Properties();

     public static void initComms(ServletContext sct){
try{
properties.load(sct.getResourceAsStream("/WEB-INF/conf/comms.properties"));
}catch(Exception e){
logger.error("加载comms.properties文件异常,cause:"+e.getMessage());
}
} public static String getCommsValue(String key){
return properties.getProperty(key, null);
} }

3、加载本地緩存,定时轮询刷新(定义定时线程池,1个线程)

cacheManager = (CacheManager)SpringApplicationContextUtils.getBean(EsbUtils.CACHEMANAGER);
cacheManager.loadAllCache();
 package com.zat.mesb.base;

 import com.zat.mesb.util.EsbCommsUtils;
import com.zat.sproxy.thread.NamedThreadFactory;
import org.springframework.stereotype.Controller; import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; @Controller
public class CacheManager { private List<AbstractCache> listCaches;

// 定义定时线程池,1个线程
18 private final ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1, new NamedThreadFactory("reloadcache", true)); public List<AbstractCache> getListCaches() {
return listCaches;
} public void setListCaches(List<AbstractCache> listCaches) {
this.listCaches = listCaches;
}

    // 定时查询参数
28 public void loadAllCache() {
29 //loadCache();
30 this.scheduled.scheduleWithFixedDelay(new Runnable() {
31
32 @Override
33 public void run() {
34 loadCache();
35 }
36 }, 1L, Long.valueOf(EsbCommsUtils.getCommsValue("flush.cache.data")), TimeUnit.SECONDS);
37 } private void loadCache() {
if(this.listCaches != null){
for(AbstractCache cache : listCaches) {
cache.loadCache();
}
}
} public Object getCacheBySimpleClassName(String className){
if(this.listCaches != null){
for(AbstractCache cache : listCaches){
if(cache.getClass().getSimpleName().equalsIgnoreCase(className)){
return cache.getCacheList();
}
}
}
return null;
} public Object getCacheValueByKey(String className, String key){
if(this.listCaches != null){
for(AbstractCache cache : listCaches){
if(cache.getClass().getSimpleName().equalsIgnoreCase(className)){
return cache.cacheMaps.get(key);
}
}
}
return null;
} public Object getCacheValueByKey(String className, String key, String type){
if(this.listCaches != null){
for(AbstractCache cache : listCaches){
if(cache.getClass().getSimpleName().equalsIgnoreCase(className)){
return cache.getCacheMaps().get(key);
}
}
}
return null;
} public void clearCache(){
if(this.listCaches != null){
for(AbstractCache cache : listCaches){
cache.clearCache();
}
}
} }

完整示例代码:

 package com.zat.mesb.listener;

 import com.zat.mesb.base.CacheManager;
import com.zat.mesb.stage.processor.StageProcessorManager;
import com.zat.mesb.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.ContextLoaderListener; import javax.servlet.ServletContextEvent; public class EsbListener extends ContextLoaderListener { private static Log logger = LogFactory.getLog(EsbListener.class);
private CacheManager cacheManager; @Override
public void contextDestroyed(ServletContextEvent sce) {
super.contextDestroyed(sce);
} @Override
public void contextInitialized(ServletContextEvent event) {
super.contextInitialized(event);
logger.info("1.开始加载spring上下文...");
SpringApplicationContextUtils.initApplicationContext(event.getServletContext());
EsbCommsUtils.initComms(event.getServletContext());
logger.info("1.加载spring上下文完成..."); logger.info("2.开始加载本地緩存...");
cacheManager = (CacheManager)SpringApplicationContextUtils.getBean(EsbUtils.CACHEMANAGER);
cacheManager.loadAllCache();
logger.info("2.加载本地緩存完成..."); logger.info("3.开始加載BusHandlers配置信息...");
BusHandlerUtils.initBusHandlers(event.getServletContext());
logger.info("3.加載BusHandlers配置信息完成..."); logger.info("4.开始加載ApiHandlers配置信息...");
ApiHandlerUtils.initApiHandlers(event.getServletContext());
logger.info("4.加載ApiHandlers配置信息完成..."); logger.info("5.开始加載ApiAlipayHandlers配置信息...");
ApiAlipayHandlerUtils.initApiAlipayHandlers(event.getServletContext());
logger.info("5.加載ApiAlipayHandlers配置信息完成..."); logger.info("6.開始初始化業務階段流程...");
Thread thread = StageProcessorManager.getInstance();
thread.setPriority(Thread.MAX_PRIORITY);
thread.start();
if(thread != null && thread.isAlive()){
try {
thread.join();
} catch (InterruptedException e) {
logger.error("Init stage process error,cause:"+e.getMessage());
}
}
logger.info("6.初始化業務階段流程完成...");
} }