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文件 ...
随机推荐
- CSS之Flex 布局
Flex 布局教程:语法篇 网页布局(layout)是 CSS 的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + float属性.它对于那些 ...
- js中contains()方法的了解
今天第一次碰到了contains()方法,处于好奇了解了一下:发现在某些场合还是挺有用的. contains(),js原生方法,用于判断DOM元素的包含关系: 需要注意的是:它以HTMLElement ...
- JavaWeb学习总结(八):HttpServletRequest对象
一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...
- Java 之集合框架 上(9)
Java 中的集合框架 如果一个类中存在很多相同类型的属性. 例如:学生类 学生可以选课,因此存在很多课程类型的属性.但是每个学生选择的课程的种类和数量是不一样的. 如果将每一个课程类型的属性都列到课 ...
- 平均负载(Load average)
load average 的含义平均负载(load average)是指系统的运行队列的平均利用率,也可以认为是可运行进程的平均数. top命令中load average显示的是最近1分钟.5分钟和1 ...
- c#(winform)环境下使用动态链接库dll的详解
1,什么是dll文件? DLL(Dynamic Link Library)文件为动态链接库文件,又称“应用程序拓展”,是软件文件类型.在Windows中,许多应用程序并不是一个完整的可执行文件,它们被 ...
- FTP自动上传
注:该脚本上传的压缩文件,通过文件资源管理器拿下来后会提示压缩文件损坏.使用 ftp -s:d:\audit_log\ftp_upload.txt 方法可行(http://www.cnblogs.co ...
- ubuntu13.04更新源
最近163的源出问题了,又要换一次源. 报错如下: 查更新源的命令查了好多次,这次还是记下来吧,估计以后还会用到很多次. 常规来说,是要先备份的,不过感觉备份也没什么用,所以就直接跳过吧.. 1 打 ...
- webpack笔记一 起步
webpack笔记一 起步 安装 对于大多数项目,我们建议本地安装(--save-dev).这可以在引入突破式变更(breaking change)版本时,更容易分别升级项目. 起步 初始化项目 mk ...
- oracle 查询当前正在执行的事务
SELECT s.sid, s.serial#, s.event, a.sql_text, a.sql_fulltext, s.username, s.status, s.machine, s.ter ...