Spring 启动加载资源到内存
前言
在一些业务场景中,当容器初始化完成之后,需要处理一些操作,比如一些数据的加载、初始化缓存、特定任务的注册等等。我找到了三种方式解决下面的问题。
1、使用PostConstruct注解
这种解决方法比较适用于: 在对于接口响应时间要求比较短,而接口中又需要频繁调用数据库查询,或者调用外部系统的情况下,为了加快接口的响应速度,在项目启动时,将通过途径获取的结果初始化到静态变量或者放置到各种缓存中。
首先静态代码块不行,静态代码块的执行顺序在Spring注解之前,当执行的时候,调用请求的Service还没有注入进来,执行时会报空指针错误。
在启动时完成一些初始化的操作,而这些初始化的操作,又要依赖于依赖注入的结果,就无法在构造方法中实现了。为此需要使用@PostConstruct,在构造方法之后执行,被@PostConstruct注解的方法会依赖完成后被自动调用。
关于Constructor,Spring注解,@PostConstruct执行顺序:Constructor >Spring注解> @PostConstruct
@Component
public class TestUtils { @Autowired
ICommonService commonService; public static List<Rsp> resourceList = new ArrayList<>(); //初始化的全局静态变量
private static ICommonService reCommonService; @PostConstruct
public void Init() {
//查询所有的
Req reqBean = new Req();
reCommonService=commonService;
reqBean.setFindType(PROVINCE_CITY_FIND_TYPE.FIND_TYPE_B.toString());//查询所有 城市
Rsp rsp=reCommonService.getPaAreaCityInfo(reqBean);
resourceList.add(rsp);
}
}
2、使用ApplicationListener
在初始化缓存或者特定任务的注册的场景下,可以使用Spring提供的ApplicationListener来进行操作。
首先,要实现ApplicationListener接口并实现onApplicationEvent方法。举例(使用SpringBoot)如下:
public class ApplicationEventListener implements ApplicationListener<ApplicationEvent> {
@Autowired
private PubSubService pubSubService; @Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent) {
ApplicationContext applicationContext = ((ContextRefreshedEvent) event).getApplicationContext();
GlobolParameters.context = applicationContext;
BaseLogger.info("ContextRefreshed初始化开始...");
// 设置上下文
}
if (event instanceof ApplicationReadyEvent) {
//启动发布订阅
pubSubService = GlobolParameters.context.getBean(PubSubService.class);
// redis subscribe job
pubSubService.subscribe();
// redis publish job
pubSubService.publish();
}
if (event instanceof ApplicationFailedEvent) {
Throwable exception = ((ApplicationFailedEvent) event).getException();
BaseLogger.error("【ApplicationFailedEvent】Spring IOC init error ", exception);
}
}
}
然后实例化ApplicationEventListener类,在Spring Boot中通过一个配置类进行实例化:
@Configuration
public class ListenerConfig {
@Bean
public ApplicationEventListener applicationEventListener(){
return new ApplicationEventListener();
}
}
通过上述代码,在SpringListener中做到,启动了发布订阅的服务。
3、启动类显式调用
如果对外调用的服务,需要依赖启动后放置在容器中的上下文的情况,则需要手动在启动类中获取相应的Bean,调用初始化的方法进行初始化。
public class StartIiApplication{
public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(StartIiApplication.class, args);
BeanUtil.setApplicationContext(applicationContext);
//从启动的容器中获取Bean,并调用初始化方法
TestUtils util=(TestUtils) applicationContext.getBean("testUtils");
util.Init(); }
}
Spring 启动加载资源到内存的更多相关文章
- spring启动加载过程源码分析
我们知道启动spring容器两常见的两种方式(其实都是加载spring容器的xml配置文件时启动的): 1.在应用程序下加载 ApplicationContext ctx = new ClassPat ...
- as3 Loader 加载资源后内存泄露无法释放的问题。
本人用Loader加载外部一个swf.之后unloadAndStop,Flash概要分析发现,内存没有被释放. 网上搜了一大堆文章,要么就是加载bitmapdata之后,自己dispose,要么就是加 ...
- 解析spring启动加载dubbo过程
一:简单配置 web.xml <context-param> <param-name>contextConfigLocation</param-name> < ...
- tomcat启动加载web项目内存溢出
通过tomcat命令启动tomcat的web项目时,根据项目大小,有可能会报以下两个错误. 在启动时没有错误,但是在访问时会报错: 1. java.lang.OutOfMemoryError: Jav ...
- spring启动加载类,手动加载bean
方法一: public final class Assembler implements BeanFactoryPostProcessor { private static ConfigurableL ...
- Aery的UE4 C++游戏开发之旅(4)加载资源&创建对象
目录 资源的硬引用 硬指针 FObjectFinder<T> / FClassFinder<T> 资源的软引用 FSoftObjectPaths.FStringAssetRef ...
- Spring 启动时加载资源
Spring加载资源文件目前了解三种, @PostConstruct在Context加载完成之后加载.在创建各个Bean对象之前加载. 实现ApplicationRunner的run方法,Bean加载 ...
- Spring boot 国际化自动加载资源文件问题
Spring boot 国际化自动加载资源文件问题 最近在做基于Spring boot配置的项目.中间遇到一个国际化资源加载的问题,正常来说只要在application.properties文件中定义 ...
- spring加载资源文件中classpath*与classpath的区别
在spring和MyBatis继承的时候,配置mapperLocations.一开始配置是这样的. 需要加载路径为com/thomas/base/mapper和com/thomas/bu/mapper ...
随机推荐
- Tkinter 之主窗口参数
一.常用参数 语法 作用 window= tk.TK() 创建窗口 window['height'] = 300 设置高 window['width'] = 500 设置宽 window.title( ...
- SSM框架的配置Spring+Springmvc +Mybatis
ssm框架是由spring mvc +spring+mybatis组成 快速阅读 通过spring的配置文件spring.xml,在servlet中指定spring mvc的配置文件spring-mv ...
- 更换镜像加快python pip 安装扩展库的速度
一些镜像源: 清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/ 阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科 ...
- Filename too long Resolution
在git bash中,运行下列命令: git config --global core.longpaths true --global是该参数的使用范围,如果只对本版本库设置该参数,只要在上述命令中去 ...
- SpringBoot2.X中的静态资源访问失效
今天开始使用SpringBoot写项目,于是先让其能够访问静态资源,但是配置半天也不能访问我的helloWorld页面,原来,在SpringBoot2.x中,一下静态资源路径不生效了. classpa ...
- leetcode 127. Word Ladder、126. Word Ladder II
127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...
- android APP国际化一键切换实现
首先看目录: 上代码: package com.loaderman.language; import android.content.res.Configuration; import android ...
- hadoop目录命令
下面是经常使用到的,以此记录备忘 1.查看hadoop目录 命令: hadoop fs -ls / 2.创建目录 命令:hadoop fs -mkdir /目录名 3.将文件上传hadoop中(也就是 ...
- Linux -- Proactor(及其与Reactor的比较)
高并发服务器常由多线程+IO复用服务器(one event loop per thread) 两种I/O多路复用模式:Reactor和Proactor 一般地,I/O多路复用机制都依赖于一个事件多路分 ...
- Python使用设计模式中的责任链模式与迭代器模式的示例
Python使用设计模式中的责任链模式与迭代器模式的示例 这篇文章主要介绍了Python使用设计模式中的责任链模式与迭代器模式的示例,责任链模式与迭代器模式都可以被看作为行为型的设计模式,需要的朋友可 ...