spring bean容器加载后执行初始化处理@PostConstruct
先说业务场景,我在系统启动后想要维护一个List常驻内存,因为我可能经常需要查询它,但它很少更新,而且数据量不大,明显符合缓存的特质,但我又不像引入第三方缓存。现在的问题是,该List的内容是从数据库中查到的,那么如何实现在spring bean加载后(数据源这时已加载),才去初始化这个List呢?用@PostConstruct这个注解就好了,这是一个很有意思的注解,它是javax包里的注解,但spring却支持了它,其他这个注解的功能就类似于
@Bean(initMethod="init")
接下来看例子:
import com.crocodile.springboot.model.Merchant;
import com.crocodile.springboot.repository.MerchantRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List; @Component
public class MerchantList {
@Autowired
private MerchantRepository merchantRepository; private MerchantList() {
} static class SingletonHolder {
static MerchantList instance = new MerchantList();
} public static MerchantList getInstance() {
return SingletonHolder.instance;
} private static List<Merchant> merchants = new ArrayList<>(); @PostConstruct
private void init() {
merchants = (List<Merchant>) merchantRepository.findAll();
} public void addMerchant(Merchant merchant) {
merchants.add(merchant);
} public void deleteMerchant(Merchant merchant) {
merchants.remove(merchant);
} public List<Merchant> getMerchants() {
return merchants;
}
}
我们既然使用了spring的初始化处理,那么就得让它发现不是?所以@Component是少不了的。Spring的Bean生命周期简单来说即:创建Bean->Bean的属性注入->Bean初始化->Bean销毁。我们结合上面的Bean来说,MerchantList这个bean先是被Spring容器创建,当然这里也会去创建MerchantRepository这个bean,容器统一管理所有的bean。接着MerchantRepository这个bean被注入到MerchantList这个bean,接着spring发现MerchantList有个初始化注解@PostConstruct就去执行了init方法。
@PostConstruct在Spring的CommonAnnotationBeanPostProcessor类中接受处理:
public CommonAnnotationBeanPostProcessor() {
this.setOrder(2147483644);
this.setInitAnnotationType(PostConstruct.class);
this.setDestroyAnnotationType(PreDestroy.class);
this.ignoreResourceType("javax.xml.ws.WebServiceContext");
}
spring bean容器加载后执行初始化处理@PostConstruct的更多相关文章
- Spring Bean 的加载过程
Spring Bean 的加载过程 一个是populateBean,一个是initializeBean,这两个方法完成了bean的赋值与初始化. 这里有一个BeanDefinitionValueRes ...
- spring boot容器加载完后执行特定操作
有时候我们需要在spring boot容器启动并加载完后,开一些线程或者一些程序来干某些事情.这时候我们需要配置ContextRefreshedEvent事件来实现我们要做的事情 1.Applicat ...
- Spring Bean 的加载顺序
一,单一Bean 装载 1. 实例化; 2. 设置属性值; 3. 如果实现了BeanNameAware接口,调用setBeanName设置Bean的ID或者Name; 4. 如果实现BeanFacto ...
- Spring源码:Spring IoC容器加载过程(1)
Spring源码版本:4.3.23.RELEASE 一.加载过程概览 Spring容器加载过程可以在org.springframework.context.support.AbstractApplic ...
- Spring Bean配置加载为BeanDefinition全过程(注解配置)
生产中有很多形式的的配置方式,本文仅分析注解配置.对于其他形式的配置区别主观以为主要在配置文件的解析过程不同,不一一分析了.本文以利用Dubbo框架开发rpc服务端为例详细阐述配置类的解析.数据保存. ...
- Spring Bean的加载
Spring加载bean的原则:不等bean创建完成就会将创建bean的ObjectFactory提早曝光加入到缓存中. 单例bean在Spring容器里只会创建一次,后续创建时会首先从缓存中获取 ...
- Spring源码:Spring IoC容器加载过程(2)
Spring源码版本:4.3.23.RELEASE 一.加载XML配置 通过XML配置创建Spring,创建入口是使用org.springframework.context.support.Class ...
- springboot容器加载完毕执行某一个方法
问题: 最近做项目(项目使用的是springboot)的时候,数据库有一个配置参数表,每次都要查询数据库去做数据转换,这样每次查询数据库感觉不太友好,后来写了一个方法项目启动完成后立即执行此方法,将配 ...
- BeanDefinitionLoader spring Bean的加载器
spring 容器注册bean , 会把bean包装成beanDefinition 放进spring容器中,beanDefinitionLoader就是加载bean的类 . 一.源码 class Be ...
随机推荐
- SpringBoot之多数据源动态切换数据源
原文:https://www.jianshu.com/p/cac4759b2684 实现 1.建库建表 首先,我们在本地新建三个数据库名分别为master,slave1,slave2,我们的目前就是写 ...
- linux网络编程之posix信号量与互斥锁
继上次学习了posix线程之后,这次来讨论一下posix信号量与互斥锁相关的知识: 跟posix消息队列,共享内存的打开,关闭,删除操作一样,不过,上面的函数是对有名信号量进行操作,通过man帮助可以 ...
- Mybatis3.1-[tp-30-31]-select_resultMap_关联查询_级联属性封装结果__association定义关联对象封装规则
笔记要点 出错分析与总结 在全局配置中,映射dao包下的全部: <mapper> <package name="com.dao"/> </mapper ...
- vue2 单一事件中心管理组件通信
- C#获取资源文件
System.Resources.ResourceManager rs = new System.Resources.ResourceManager("NetWebBrowser.Resou ...
- HttpReader
头文件: #pragma once #include <afxinet.h> class CSF_HttpDataReader { public: CSF_HttpDataReader(v ...
- learning scala Function Composition andThen
Ordering using andThen: f(x) andThen g(x) = g(f(x)) Ordering using compose: f(x) compose g(x) = f(g( ...
- 主席树K-th Number
/*K-th NumberTime Limit: 20000MS Memory Limit: 65536KTotal Submissions: 44535 Accepted: 14779Case Ti ...
- [转]CentOS 7安装Python3.6过程(让linux系统共存Python2和Python3环境)
CentOS 7系统自带了python2,不过可以不用2版本,直接使用python3运行python脚本就可以,但是千万别去动系统自带的python2,因为有程序依赖目前的python2环境,比如yu ...
- [转] C++ STL中map.erase(it++)用法原理解析
总结一下map::erase的正确用法. 首先看一下在循环中使用vector::erase时我习惯的用法: for(vector<int>::iterator it = vecInt.be ...