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.初始化業務階段流程完成...");
} }
1.Spring项目启动时,加载相关初始化配置的更多相关文章
- spring项目中监听器作用-ContextLoaderListener(项目启动时,加载一些东西到缓存中)
作用:在启动Web容器时,自动装配Spring applicationContext.xml的配置信息. 因为它实现了ServletContextListener这个接口,在web.xml配置这个监听 ...
- ElasticSearch 启动时加载 Analyzer 源码分析
ElasticSearch 启动时加载 Analyzer 源码分析 本文介绍 ElasticSearch启动时如何创建.加载Analyzer,主要的参考资料是Lucene中关于Analyzer官方文档 ...
- 微服务架构 | *2.3 Spring Cloud 启动及加载配置文件源码分析(以 Nacos 为例)
目录 前言 1. Spring Cloud 什么时候加载配置文件 2. 准备 Environment 配置环境 2.1 配置 Environment 环境 SpringApplication.prep ...
- Servlet在启动时加载的tomcat源码(原创)
tomcat 8.0.36 知识点: 通过配置loadOnStartup可以设置Servlet是否在Tomcat启动时加载,以及按值大小进行有序加载,其最小有效值为0,最大有效值为Integer.MA ...
- web.xml中配置启动时加载的servlet,load-on-starup
web.xml中配置启动时加载的servlet,load-on-starup 使用servlet来初始化配置文件数据: 在servlet的配置当中,<load-on-startup>1&l ...
- 依赖Spring的情况下,Java Web项目如何在启动时加载数据库中的数据?
原文:https://blog.csdn.net/u012345283/article/details/39558537 原文:https://blog.csdn.net/wandrong/artic ...
- stark组件前戏之项目启动前加载指定文件
1. django项目启动时, 自定制执行某个py文件 dajngo 启动时.会将所有 路由加载到内存中. 我的目的就是在 路由加载之前,执行某个py文件. 每个app中都有一个 apps.py fr ...
- 原生servlet项目启动自动加载一个方法
web.xml里的配置: 配置好要加载的类,其中1这一句是项目启动时自动加载该类的必要条件. <servlet> <servlet-name>SharePltfCLServle ...
- stark组件前戏(1)之项目启动前加载指定文件
django项目启动时,可以自定义执行某个py文件,这需要在任意app的apps.py中的Config类定义ready方法,并调用. from django.apps import AppConf ...
随机推荐
- CH5105 Cookies饼干(线性DP)
题意理解 圣诞老人共有\(M\)个饼干,准备全部分给\(N\)个孩子. 每个孩子有一个贪婪度,第 i 个孩子的贪婪度为 \(g[i]\). 如果有 \(a[i]\) 个孩子拿到的饼干数比第 \(i\) ...
- 【墨西哥区域赛】Carpet
原题: 题意: 给你一个树,有1e5个节点,让你把这个树放在一个长1e6宽20的网格图里,要求一个格子放一个节点,树边之间不能相交 这是一道构造题 因为树的形状可能性很多,很复杂,所以不能简单猜测,而 ...
- VSCode远程连接Docker
一.Docker开启远程访问 [root@local host ~]# vi /lib/systemd/system/docker.service #修改ExecStart这行 ExecStart=/ ...
- pip安装第三方库时提示No Module named pip
因为多次卸载/安装python不同版本导致出现pip模块不存在的问题,原因可能是环境没有配置好执行下面命令 ----完成
- 关于上部盒子里有图片下面盒子magin-top负值的层级问题
.box{ width: 800px; box-sizing: border-box; margin: 50px auto 0; background: pink; } .imgBox{ width: ...
- Java 对象序列化和反序列化 (实现 Serializable 接口)
序列化和反序列化的概念 把对象转换为字节序列的过程称为对象的序列化. 把字节序列恢复为对象的过程称为对象的反序列化. 对象的序列化主要有两种用途: 1) 把对象的字节序列永久地保存到硬盘上,通常存放 ...
- JSP页面的Page指令指定编码和Meta标签编码
JSP代码如下: <%@ page language="java" contentType="text/html; charset=UTF-8" page ...
- AcWing:99. 激光炸弹(前缀和)
一种新型的激光炸弹,可以摧毁一个边长为 RR 的正方形内的所有的目标. 现在地图上有 NN 个目标,用整数Xi,YiXi,Yi表示目标在地图上的位置,每个目标都有一个价值WiWi. 激光炸弹的投放是通 ...
- MySQL_(Java)使用JDBC向数据库中删除(delete)数据
MySQL_(Java)使用JDBC向数据库发起查询请求 传送门 MySQL_(Java)使用JDBC向数据库中插入(insert)数据 传送门 MySQL_(Java)使用JDBC向数据库中删除(d ...
- Vue_(组件通讯)单项数据流
Vue单项数据流 传送门 单向数据流:父组件值的更新,会影响到子组件,反之则不行 修改子组件的值: 局部数据:在子组件中定义新的数据,将父组件传过来的值赋值给新定义的数据,之后操作这个新数据 如果对数 ...