Spring中的一些常用接口
一、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中的一些常用接口的更多相关文章
- 由一个RABBITMQ监听器死循环引出的SPRING中BEAN和MAPPER接口的注入问题
1 @Slf4j 2 @RestController 3 @Component 4 public class VouchersReceiverController implements Message ...
- Spring中配置数据源常用的形式
不管采用何种持久化技术,都需要定义数据源.Spring中提供了4种不同形式的数据源配置方式: spring自带的数据源(DriverManagerDataSource),DBCP数据源,C3P0数据源 ...
- spring中bean的常用属性
一.scop scope用来配置bean对象是否是单例模式.单例模式是java的二十三种设置模式之一,指在这个项目运行过程中一 个类的对象只会实例化一次.一般,工厂类的对象都是单例模式.非单例模式叫多 ...
- Spring中的可扩展接口
1.监听器Listener(点此连接,执行流程带源码分析及demo) 2.bean定义的后置处理器(BeanDefinitionRegistryPostProcessor)和bean工厂的后置处理器( ...
- Spring中常用的23中设计模式
1.spring 中常用的设计模式有23中 分类 设计模式 创建型 工厂方法模式(FactoryMethod).抽象工厂模式(AbstractFactory).建造者模式(Builder).原型 ...
- MyBatis-Spring中间件逻辑分析(怎么把Mapper接口注册到Spring中)
1. 文档介绍 1.1. 为什么要写这个文档 接触Spring和MyBatis也挺久的了,但是一直还停留在使用的层面上,导致很多时候光知道怎么用,而不知道其具体原理,这样就很难做一 ...
- Spring中的设计模式学习
Spring提供了一种Template的设计哲学,包含了很多优秀的软件工程思想. 1. 简单工厂 又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一. ...
- Spring中的设计模式
[Spring中的设计模式] http://www.uml.org.cn/j2ee/201301074.asp [详解设计模式在Spring中的应用] [http://www.geek521.c ...
- Spring中对资源的读取支持
Resource简单介绍 注:所有操作基于配置好的Spring开发环境中. 在Spring中,最为核心的部分就是applicationContext.xml文件,而此配置文件中字符串的功能发挥到了极致 ...
随机推荐
- String的split(String regex, int limit)方法小结
split(String regex, int limit)方法,头一个参数String regex表示字符串分割的模式,包括分隔符和正则表达式:但是第二个参数limit比较迷糊人,api中这样解释: ...
- windows下启动Apache报443错误!
windows下启动apache报make_sock: could not bind to address [::]:443错误! 查看指定端口的占用情况 netstat -aon|findstr & ...
- NSNumber数字
前言 将基本数据类型包装成 OC 对象 1.NSNumber 与 基本数据类型 的相互转换 // 基本数据类型 转 NSNumber // 对象方法,将整形数据转换为 OC 对象 NSNumber * ...
- POJ - 2528Mayor's posters (离散化+线段树区间覆盖)
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...
- 微信Dat文件解码
最近在整理磁盘文件,因为经过一段时间的蹂躏后,磁盘实在是太多东西了,不整理一下,简直对不住我的SSD好嘛.偶然发现磁盘中某公司的文件夹占用空间简直不能再大,那可是我的C盘啊,合计才119GB的SSD空 ...
- [ZJOI2009]狼和羊的故事 BZOJ1412
题目描述 “狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向......” Orez听到这首歌,心想:狼和羊如此和谐,为什么不尝试羊狼合养呢?说干就干! Orez的羊狼圈 ...
- kuangbin专题七 POJ3468 A Simple Problem with Integers (线段树或树状数组)
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- 使用css实现垂直居中
vartical-align vartical-align可以设置行内元素和表格单元格(table-cell)垂直对方式,所以如果元素是行内元素或者表格的话,可以直接应用这个属性对内容进行对齐设置.如 ...
- C#学习之按钮点击事件
描述:asp.net中服务器控件Button的点击事件OnClientClick和OnClick的区别? 解答:http://www.cnblogs.com/ypfnet/archive/2012/1 ...
- Luogu P2107 小Z的AK计划 堆贪心
好久不做这种题了... 存一下每个点的位置和时间,由于达到某个位置跟之前去哪里AK的无关,所以在时间超限后,可以用大根堆弹掉之前消耗时间最大的,来更新答案,相当于去掉之前花费最大的,直到时间不在超限. ...