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 ...
随机推荐
- mybatis配置加载源码概述
Mybatis框架里,有两种配置文件,一个是全局配置文件config.xml,另一个是对应每个表的mapper.xml配置文件.Mybatis框架启动时,先加载config.xml, 在加载每个map ...
- phpStudy环境下composer的安装
前言 原来是做php开发的,现在转行前端工程师,因为很久没有接触了,可能会有其他问题,这里简单记录一下,后期遇到什么问题再进行更新~ 话说下载特别慢所以这里给个网盘链接Composer-Setup.e ...
- Redis和Memcache区别
1. Redis和Memcache都是将数据存放在内存中,都是内存数据库.不过memcache还可用于缓存其他东西,例如图片.视频等等. 2.Redis不仅仅支持简单的k/v类型的数据,同时还提供li ...
- cookie生命周期expires 时间过期,但是cookie没有被浏览器清理的问题
cookie生命周期expires 时间过期,但是cookie没有被浏览器清理的问题 今天用nodejs起了一个web服务,使用art-template模板引擎渲染出来的页面,在这里我设置了一个coo ...
- java+上传大视频文件断点续传
上周遇到这样一个问题,客户上传高清视频(1G以上)的时候上传失败. 一开始以为是session过期或者文件大小受系统限制,导致的错误. 查看了系统的配置文件没有看到文件大小限制, web.xml中s ...
- C/C++ - malloc/free和new/delete的区分
new/delete与malloc/free的区别主要表现在以下几个方面: 注意:最主要的区别,new/delete是运算符,而malloc/free是函数 (1).new能够自动计算需要分配的内存空 ...
- TTTTTTTTTTTTTTTTTTT UVA 2045 Richness of words
J - Richness of words Time Limit:500MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64 ...
- Java线程之FutureTask
简述 FutureTask是Future接口的实现类,并提供了可取消的异步处理的功能,它包含了启动和取消(start and cancel)任务的方法,同时也包含了可以返回FutureTask状态(c ...
- CentOS7 安装JumpServer
环境: CentOS Linux release 7.6.1810 (Core) JumpServer 1.4.8 Python 3.6.X MariaDB 编译安装Python3.6 首先,下载Py ...
- 在CentOS 7上搭建WordPress
环境(ECS阿里云服务器) 服务器操作系统:CentOS 7.3 : 博客部署服务器:Apache HTTP: 数据库:MySql: 框架:WordPress: 步骤 一.安装 Apache HTTP ...