一、ApplicationContextAware接口

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

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

二、ServletContextAware 接口

public interface ServletContextAware extends Aware {
void setServletContext(ServletContext var1);
}

三、InitializingBean 接口

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

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

public interface InitializingBean {
void afterPropertiesSet() throws Exception;
}

四、ApplicationListener<ApplicationEvent> 接口

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

执行顺序

@Component
public class StartupListener implements ApplicationContextAware, ServletContextAware,
InitializingBean, ApplicationListener<ContextRefreshedEvent> { protected Logger logger = LogManager.getLogger(); @Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
logger.info("1 => StartupListener.setApplicationContext");
} @Override
public void setServletContext(ServletContext context) {
logger.info("2 => StartupListener.setServletContext");
} @Override
public void afterPropertiesSet() throws Exception {
logger.info("3 => StartupListener.afterPropertiesSet");
} @Override
public void onApplicationEvent(ContextRefreshedEvent evt) {
logger.info("4.1 => MyApplicationListener.onApplicationEvent");
if (evt.getApplicationContext().getParent() == null) {
logger.info("4.2 => MyApplicationListener.onApplicationEvent");
}
} }

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

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. 【转】链接任意目录下库文件(解决错误“/usr/bin/ld: cannot find -lxxx”

    netbeans构建项目也出现了同样的问题.猜测是netbeans内部就用的是-l 这种编译方式,所以需要把***.a手动改为lib***.a 原文地址:链接任意目录下库文件(解决错误“/usr/bi ...

  2. 无废话MVC入门教程笔记

    自学mvc,看了园子里李林峰写的李林峰写的无废话MVC入门教程笔记,现在有的平时忽略的或是不太清楚的点记下来 1,Html.DropDownList //服务端写法 @{ //下拉列表的值 List& ...

  3. Wannafly挑战赛28B(DP,思维,字符串)

    #include<bits/stdc++.h>using namespace std;int n;int nxt[3][100007];char buff[100007];const ch ...

  4. 学习高性能mysql

    Mysql 的InnoDB存储引擎实现的不是简单的行级锁,实现的是MVCC,多版本并发控制,可以理解成行级锁的一个变种. InnoDB的MVCC是通过在每行纪录后面保存两个隐藏的列来实现的.这两个列, ...

  5. 高级工程师->架构师

    1. 分解等级 技术人员典型的发展路径基本上都是下面的这个模式: 1) 0 ~1年:菜鸟,需要别人手把手来教 2)1 ~ 3年:初级,需要别人带你做 3)3 ~ 5年:高级,能独当一面,可以带初级技术 ...

  6. 洛谷 P3806 【模板】点分治1

    P3806 [模板]点分治1 题目背景 感谢hzwer的点分治互测. 题目描述 给定一棵有n个点的树 询问树上距离为k的点对是否存在. 输入输出格式 输入格式: n,m 接下来n-1条边a,b,c描述 ...

  7. left jion on和where条件的区别

    1.on是在生成临时表时()起作用,而且不管on中的条件是否为真,都会返回(left join)左边所有的数据,如果不匹配也是返回空. 2.where 是在生成了临时表后,再对表进行过滤 个人理解:先 ...

  8. phantomjs截图中文网站网页页面乱码,安装字体解决

    用phantomjs去截取中文页面的网站可能会出现乱码的情况,也就是截图中中文的位置全是方框. 解决办法就是安装字体. 在centos中执行:yum install bitmap-fonts bitm ...

  9. 10大Python开源项目推荐(Github平均star2135)

    翻译 | suisui 来源 | 人工智能头条(AI_Thinker) 继续假日充电系列~本文是 Mybridge 挑选的 10 个 Python 开源项目,Github 平均star 2135,希望 ...

  10. C++ 标准库智能指针

    整理一下c++中shared_ptr,weak_ptr,unique_ptr三种指针的使用案例和注意事项,让程序资源更加案例,在标准库中,需要包含<memory>,在boost库中, 一. ...