一、ApplicationContextAware接口

注:可以在spring容器初始化的时候调用setApplicationContext方法,从而获得ApplicationContext中的所有bean。

  1. public interface ApplicationContextAware extends Aware {
  2. void setApplicationContext(ApplicationContext var1) throws BeansException;
  3. }

二、ServletContextAware 接口

  1. public interface ServletContextAware extends Aware {
  2. void setServletContext(ServletContext var1);
  3. }

三、InitializingBean 接口

注:这个方法将在所有的属性被初始化后调用,但是会在init前调用,

在spring初始化bean的时候,如果该bean是实现了InitializingBean接口,并且同时在配置文件中指定了init-method,系统则是先调用afterPropertiesSet方法,然后在调用init-method中指定的方法。

  1. public interface InitializingBean {
  2. void afterPropertiesSet() throws Exception;
  3. }

四、ApplicationListener<ApplicationEvent> 接口

  1. public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
  2. void onApplicationEvent(E var1);
  3. }

执行顺序

  1. @Component
  2. public class StartupListener implements ApplicationContextAware, ServletContextAware,
  3. InitializingBean, ApplicationListener<ContextRefreshedEvent> {
  4.  
  5. protected Logger logger = LogManager.getLogger();
  6.  
  7. @Override
  8. public void setApplicationContext(ApplicationContext ctx) throws BeansException {
  9. logger.info("1 => StartupListener.setApplicationContext");
  10. }
  11.  
  12. @Override
  13. public void setServletContext(ServletContext context) {
  14. logger.info("2 => StartupListener.setServletContext");
  15. }
  16.  
  17. @Override
  18. public void afterPropertiesSet() throws Exception {
  19. logger.info("3 => StartupListener.afterPropertiesSet");
  20. }
  21.  
  22. @Override
  23. public void onApplicationEvent(ContextRefreshedEvent evt) {
  24. logger.info("4.1 => MyApplicationListener.onApplicationEvent");
  25. if (evt.getApplicationContext().getParent() == null) {
  26. logger.info("4.2 => MyApplicationListener.onApplicationEvent");
  27. }
  28. }
  29.  
  30. }

运行时,输出的顺序如下:

1 => StartupListener.setApplicationContext
2 => StartupListener.setServletContext
3 => StartupListener.afterPropertiesSet
4.1 => MyApplicationListener.onApplicationEvent
4.2 => MyApplicationListener.onApplicationEvent
4.1 => MyApplicationListener.onApplicationEvent

Spring中的一些常用接口的更多相关文章

  1. 由一个RABBITMQ监听器死循环引出的SPRING中BEAN和MAPPER接口的注入问题

    1 @Slf4j 2 @RestController 3 @Component 4 public class VouchersReceiverController implements Message ...

  2. Spring中配置数据源常用的形式

    不管采用何种持久化技术,都需要定义数据源.Spring中提供了4种不同形式的数据源配置方式: spring自带的数据源(DriverManagerDataSource),DBCP数据源,C3P0数据源 ...

  3. spring中bean的常用属性

    一.scop scope用来配置bean对象是否是单例模式.单例模式是java的二十三种设置模式之一,指在这个项目运行过程中一 个类的对象只会实例化一次.一般,工厂类的对象都是单例模式.非单例模式叫多 ...

  4. Spring中的可扩展接口

    1.监听器Listener(点此连接,执行流程带源码分析及demo) 2.bean定义的后置处理器(BeanDefinitionRegistryPostProcessor)和bean工厂的后置处理器( ...

  5. Spring中常用的23中设计模式

    1.spring 中常用的设计模式有23中  分类  设计模式  创建型 工厂方法模式(FactoryMethod).抽象工厂模式(AbstractFactory).建造者模式(Builder).原型 ...

  6. MyBatis-Spring中间件逻辑分析(怎么把Mapper接口注册到Spring中)

    1.      文档介绍 1.1.      为什么要写这个文档 接触Spring和MyBatis也挺久的了,但是一直还停留在使用的层面上,导致很多时候光知道怎么用,而不知道其具体原理,这样就很难做一 ...

  7. Spring中的设计模式学习

    Spring提供了一种Template的设计哲学,包含了很多优秀的软件工程思想. 1. 简单工厂 又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一. ...

  8. Spring中的设计模式

    [Spring中的设计模式] http://www.uml.org.cn/j2ee/201301074.asp [详解设计模式在Spring中的应用]    [http://www.geek521.c ...

  9. Spring中对资源的读取支持

    Resource简单介绍 注:所有操作基于配置好的Spring开发环境中. 在Spring中,最为核心的部分就是applicationContext.xml文件,而此配置文件中字符串的功能发挥到了极致 ...

随机推荐

  1. 2015年第六届蓝桥杯省赛T10 生命之树(树形dp+Java模拟vector)

    生命之树 在X森林里,上帝创建了生命之树. 他给每棵树的每个节点(叶子也称为一个节点)上,都标了一个整数,代表这个点的和谐值. 上帝要在这棵树内选出一个非空节点集S,使得对于S中的任意两个点a,b,都 ...

  2. Java泛型读书笔记 (二)

    关于Java泛型擦除后,继承一个泛型类带来的问题 有如下泛型类Pair: public class Pair<T> { private T second; private T first; ...

  3. 【C#】 创建、部署和调用WebService的简单示例

    C# 创建.部署和调用WebService的简单示例 <div class="postBody"> <div id="cnblogs_post_body ...

  4. SqlServer压缩数据库日志

    )--数据库名称 )--数据库日志文件名称 --替换成自己的文件名称 select @dbName='dbname' select @dbNamelog='dbname_log' ) set @sql ...

  5. C# GDI

    绘制实心矩形 using (Graphics gp = Graphics.FromImage(bmBlank)) { //... ; Rectangle rec = , y, , );//画一个白块, ...

  6. 小 M 的算式(dfs)

    [问题描述]小 M 在做数学作业的时候遇到了一个有趣的问题:有一个长度为 n 的数字串 S,小 M 需要在数字之间填入若干个“+”和恰好一个“=”,使其成为一个合法的等式.如对于 S=“2349”,可 ...

  7. python---信用卡ATM

    一  需求 模拟实现一个ATM + 购物商城程序 额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 每月22号出账单,每月10号为还款日,过期未还, ...

  8. ssh 免密码远程登录

    背景: 公司有两台服务器A与B,经常会碰到代码中的配置文件不一致的情况...............,为了反面让两台服务器配置统一,所以需要写个shell脚本,用到的linux命令主要是scp 1.在 ...

  9. kolla出现问题时的定位方式

    前提,对官网问题的一个翻译 Troubleshooting Guide排障手册 1.Failures(失败) If Kolla fails, often it is caused by a CTRL- ...

  10. 微信小程序小结(5) -- 常用语法

    在 pages 字段的第一个页面就是这个小程序的首页(打开小程序看到的第一个页面). Page生命周期 属性 类型 描述 onLoad Function 生命周期函数--监听页面加载.一个页面只会调用 ...