先说业务场景,我在系统启动后想要维护一个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的更多相关文章

  1. Spring Bean 的加载过程

    Spring Bean 的加载过程 一个是populateBean,一个是initializeBean,这两个方法完成了bean的赋值与初始化. 这里有一个BeanDefinitionValueRes ...

  2. spring boot容器加载完后执行特定操作

    有时候我们需要在spring boot容器启动并加载完后,开一些线程或者一些程序来干某些事情.这时候我们需要配置ContextRefreshedEvent事件来实现我们要做的事情 1.Applicat ...

  3. Spring Bean 的加载顺序

    一,单一Bean 装载 1. 实例化; 2. 设置属性值; 3. 如果实现了BeanNameAware接口,调用setBeanName设置Bean的ID或者Name; 4. 如果实现BeanFacto ...

  4. Spring源码:Spring IoC容器加载过程(1)

    Spring源码版本:4.3.23.RELEASE 一.加载过程概览 Spring容器加载过程可以在org.springframework.context.support.AbstractApplic ...

  5. Spring Bean配置加载为BeanDefinition全过程(注解配置)

    生产中有很多形式的的配置方式,本文仅分析注解配置.对于其他形式的配置区别主观以为主要在配置文件的解析过程不同,不一一分析了.本文以利用Dubbo框架开发rpc服务端为例详细阐述配置类的解析.数据保存. ...

  6. Spring Bean的加载

    Spring加载bean的原则:不等bean创建完成就会将创建bean的ObjectFactory提早曝光加入到缓存中.   单例bean在Spring容器里只会创建一次,后续创建时会首先从缓存中获取 ...

  7. Spring源码:Spring IoC容器加载过程(2)

    Spring源码版本:4.3.23.RELEASE 一.加载XML配置 通过XML配置创建Spring,创建入口是使用org.springframework.context.support.Class ...

  8. springboot容器加载完毕执行某一个方法

    问题: 最近做项目(项目使用的是springboot)的时候,数据库有一个配置参数表,每次都要查询数据库去做数据转换,这样每次查询数据库感觉不太友好,后来写了一个方法项目启动完成后立即执行此方法,将配 ...

  9. BeanDefinitionLoader spring Bean的加载器

    spring 容器注册bean , 会把bean包装成beanDefinition 放进spring容器中,beanDefinitionLoader就是加载bean的类 . 一.源码 class Be ...

随机推荐

  1. tinymce + vue 富文本编辑

    用texterea最多支持换行,如果文本信息想要更加丰富一些,比如增加格式样式,比如增加图片,textarea就爱莫能助了 在网上搜寻了一番,发现tinymce是比较方便好用的一款富文本编辑 http ...

  2. K8S 1.12大特性最快最深度解析:Kubernetes CSI Snapshot(上)

    ​ 背景 许多存储系统提供了创建存储卷“快照”(snapshot)的能力,以防止数据丢失.快照可以替代传统的备份系统来备份和还原主要数据和关键数据.快照能够快速备份数据(例如,创建GCE PD快照仅需 ...

  3. SLAM、三维重建,语义相关数据集大全

    作者朱尊杰,公众号:计算机视觉life,编辑成员 一 主要针对自动驾驶: 1.KITTI数据集: http://www.cvlibs.net/datasets/kitti/index.php(RGB+ ...

  4. 微服务,开源 RPC 框架 - Spring Cloud

    Spring Cloud:国外 Pivotal 公司 2014 年对外开源的 RPC 框架,仅支持 Java 语言 Spring Cloud 利用 Spring Boot 特性整合了开源行业中优秀的组 ...

  5. postgresql —— 查看索引

    查索引 语句: SELECT tablename, indexname, indexdef FROM pg_indexes WHERE tablename = 'user_tbl' ORDER BY ...

  6. ajax二进制流乱码图片解决方法

    仅供自己参考 参考博客 在请求成功的地方 添加以下代码: var blob=new Blob(); blob=this.response; 既然二进制数据拿到了,那么要把它放在一个 html标签中,并 ...

  7. Git报错:Please tell me who you are.

    Git在提交的时候报错 Please tell me who you are. 报错 Please tell me who you are. 具体如下: 原因:明确报错.请告诉我你是谁.意思是你在提交 ...

  8. mali tbr Forward Pixel Kill

    https://community.arm.com/cfs-file/__key/communityserver-blogs-components-weblogfiles/00-00-00-20-66 ...

  9. 推荐python入门书籍(爬虫方面)

    学爬虫,需要理论与实践相结合,Python生态中的爬虫库多如牛毛,urllib.urllib2.requests.beautifulsoup.scrapy.pyspider都是爬虫相关的库,但是如果没 ...

  10. Codeforces 380E Sereja and Dividing

    题面 洛谷传送门 题解 博客 有精度要求所以只用求几十次就差不多了 CODE #include <bits/stdc++.h> using namespace std; typedef l ...