Spring@PostConstruct注解和构造方法的调用顺序
先看下@PostConstruct的注解
* The PostConstruct annotation is used on a method that needs to be executed
* after dependency injection is done to perform any initialization. This
* method MUST be invoked before the class is put into service. This
* annotation MUST be supported on all classes that support dependency
* injection. The method annotated with PostConstruct MUST be invoked even
* if the class does not request any resources to be injected. Only one
* method can be annotated with this annotation. The method on which the
* PostConstruct annotation is applied MUST fulfill all of the following
* criteria -
自己翻译一下,意思是:
PostConstruct注解用于方法上,该方法在初始化的依赖注入操作之后被执行。这个方法必须在class被放到service之后被执行,这个注解所在的类必须支持依赖注入。
父类class,被@component注解修饰,说明会被spring扫描并创建。在默认构造方法里加上输出打印,init方法被@PostConstruct修饰
@Component
public class ParentBean implements InitializingBean{ public ParentBean() {
System.out.println("ParentBean construct");
} @PostConstruct
public void init(){
System.out.println("ParentBean init");
} public void afterPropertiesSet() throws Exception {
System.out.println("ParentBean afterPropertiesSet");
} }
子类class,也被 @component注解修饰,其余配置和父类class一样
@Component
public class SonBean extends ParentBean{
public SonBean() {
System.out.println("SonBean construct");
} @PostConstruct
public void init(){
System.out.println("SonBean init");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("SonBean afterPropertiesSet");
} }
然后我们使用maven命令 jetty:run,控制台输出如下:
[INFO] Initializing Spring root WebApplicationContext
ParentBean construct
ParentBean init
ParentBean afterPropertiesSet
ParentBean construct
SonBean construct
SonBean init
SonBean afterPropertiesSet
[INFO] Set web app root system property: 'webapp.root' = [J:\androidworkspace\com\src\main\webapp\]
[INFO] Initializing log4j from [classpath:log4j.properties]
[INFO] Started SelectChannelConnector@0.0.0.0:8088
[INFO] Started Jetty Server
[INFO] Starting scanner at interval of 3 seconds.
可以看出优先执行依然是构造方法,这个是java的语言决定的,毕竟spring只是建立在java之上的框架。然后才是被PostConstruct修饰的方法,要注意的是这个方法在对象的初始化和依赖都完成之后才会执行,所以不必担心执行这个方法的时候有个别成员属性没有被初始化为null的情况发生。在init方法之后执行的才是afterPropertiesSet方法,这个方法必须实现InitializingBean接口,这个接口很多spring的bean都实现了他,从他的方法名就能看出在属性都被设置了之后执行,也属于springbean初始化方法。
其实spring提供了很多接口以供开发者在bean初始化后调用,可以在官网上查阅相关文档。
参考:https://my.oschina.net/wwwd/blog/810530
Spring@PostConstruct注解和构造方法的调用顺序的更多相关文章
- Java类加载及实例化的调用顺序
标题起得略拗口,大概意思就是说在一个Java类中,域和构造方法的调用顺序. 1. 没有继承的情况 单独一个类的场景下,初始化顺序为依次为 静态数据,继承的基类的构造函数,成员变量,被调用的构造函数. ...
- spring中bean的构造函数,Autowired(Value)注入与@PostConstruct调用顺序
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/yyysylvia/article/deta ...
- 【spring源码分析】spring和@PostConstruct注解
@PostConstruct注解好多人以为是Spring提供的.其实是Java自己的注解. Java中该注解的说明:@PostConstruct该注解被用来修饰一个非静态的void()方法.被@Pos ...
- Spring@PostConstruct和@PreDestroy注解详解
@PostConstruct注解使用 @PostConstructApi使用说明 The PostConstruct annotation is used on a method that needs ...
- Java学习笔记11---静态成员变量、静态代码块、成员变量及构造方法的初始化或调用顺序
当创建一个对象时,各种成员变量及构造方法的初始化或调用顺序是怎样的呢? (1).如果类尚未加载,则先初始化静态成员变量和静态代码块,再初始化成员变量,最后调用相应的构造方法: (2).如果类已经加载过 ...
- Spring @Cacheable注解 && 事务@Transactional 在同一个类中的方法调用不生效
@Cacheable 注解在对象内部调用不会生效 代码示例:ProductServiceImpl.java public List<ProductInfoVO> getProductLis ...
- @PostConstruct注解原理解析
所有文章 https://www.cnblogs.com/lay2017/p/11478237.html 正文 @PostConstruct注解使用简介 在了解一个东西的原理之前,我们得初步的懂得如何 ...
- 【String注解驱动开发】你了解@PostConstruct注解和@PreDestroy注解吗?
写在前面 在之前的文章中,我们介绍了如何使用@Bean注解指定初始化和销毁的方法,小伙伴们可以参见<[Spring注解驱动开发]如何使用@Bean注解指定初始化和销毁的方法?看这一篇就够了!!& ...
- Spring系列之Spring常用注解总结
传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...
随机推荐
- PHP 获取当前类名、方法名、URL地址
1.PHP获取当前类名.方法名 __CLASS__ 获取当前类名 __FUNCTION__ 当前函数名(confirm) __METHOD__ 当前方法名 (bankcard::confir ...
- CakePHP调用model类和foreach循环
1. 引入Model类 2.调用model类(Guarantee)下的getCity()方法 3.写sql语句 并返回获得值 4.foreach循环取得的城市
- python_tensorflow_Django实现逻辑回归
1.工程概要 2.data文件以及input_data文件准备 链接:https://pan.baidu.com/s/1EBNyNurBXWeJVyhNeVnmnA 提取码:4nnl 3.logiss ...
- LeetCode 533----Lonely Pixel II
问题描述 Given a picture consisting of black and white pixels, and a positive integer N, find the number ...
- LintCode2016年算法比赛----二叉树的所有路径
二叉树的所有路径 题目描述 给定一棵二叉树,找从根节点到叶子节点的所有路径 样例 给出下面这课二叉树: 1 / \ 2 3 \ 5 所有根到叶子的路径为: [ "1->2->5& ...
- MUI框架-04-切换页面头部文字重叠
MUI框架-04-切换页面头部文字重叠 从现在开始就只分享 MUI 框架开发时遇到的问题了 开发不懂请查看:官方文档 问题情况 切换页面 title 文字重叠,覆盖 截图: 解决办法 给 h1 标签加 ...
- 自学git心得-3
转眼到第三节了,我们进入分支管理. git领域里的分支可以理解为一个有安全保障的临时仓库,有时我们新修改了代码,突然发现有bug需要回到之前的版本,有时我们开发到一半,突然要出去一趟,如何安全保存当前 ...
- [翻译] IQAudioRecorderController
IQAudioRecorderController IQAudioRecorderController is a drop-in universal library allows to record ...
- [翻译] AsyncDisplayKit
AsyncDisplayKit AsyncDisplayKit is an iOS framework that keeps even the most complex user interfaces ...
- python面向对象编程(1)——基本概念,术语,self,构造器
1 python面向对象命名规范 类名通常由大写字母打头.这是惯例标准. 数据值应该使用名词作为名字,方法使用动词加对象的方式,若使用混合记法,则方法名的第一个字母首字母小写,后面的单词的首字母大写 ...