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 ...
随机推荐
- js 中判断对象是否为空
var dd = function (S_object, name) { console.log(name + '第一步执行结果:' + S_object); if (typeof S_objec ...
- codeforces 576C Points on Plane 相邻两点的欧拉距离
题意:给出n个点,要求排序后,相邻两点的欧拉距离之和小于等于2.5e9做法:由于0≤ xi, yi ≤ 1e6,所以可以将x<=1000的点分成一份,1000<x<=2000的点分成 ...
- PAT乙级1018
题目链接 https://pintia.cn/problem-sets/994805260223102976/problems/994805304020025344 题解 刚开始做很懵逼,可能并不难吧 ...
- 使用python读取配置文件并从mysql数据库中获取数据进行传参(基于Httprunner)
最近在使用httprunner进行接口测试,在传参时,用到了三种方法:(1)从csv文件中获取:(2)在config中声名然后进行引用:(3)从函数中获取.在测试过程中,往往有些参数是需要从数据库中获 ...
- Alpha个人项目测试
这个作业属于哪个课程 [课程链接][ ] 这个作业要求在哪里 [作业要求][ ] 团队名称 [山海皆可平][ ] 作业目标 对其他小组进行测试 测试报告 姓名 唐友鑫 学号 201631062121 ...
- Codeforces Round #451 (Div. 2) [ D. Alarm Clock ] [ E. Squares and not squares ] [ F. Restoring the Expression ]
PROBLEM D. Alarm Clock 题 OvO http://codeforces.com/contest/898/problem/D codeforces 898d 解 从前往后枚举,放进 ...
- Can't specify target table for update in FROM clause
UPDATE tbl SET col = ( SELECT ... FROM (SELECT.... FROM) AS x); 额外嵌套了一个 SELECT 语句 例如LeetCode 中的 Dele ...
- 【leetcode】1281. Subtract the Product and Sum of Digits of an Integer
题目如下: Given an integer number n, return the difference between the product of its digits and the sum ...
- K8S中Service
Service 的概念Kubernetes Service 定义了这样一种抽象:一个 Pod 的逻辑分组,一种可以访问它们的策略 —— 通常称为微服务. 这一组 Pod 能够被 Serv ...
- [笔记]动态规划(dynamic programming)
动态规划与分治方法都是通过组合子问题的解来求解原问题,区别在于:分治方法将问题划分为互不相交的子问题,递归求解子问题,再将它们的解组合起来,求出原问题的解.分治算法可能反复的求解某些公共子问题,从而使 ...