先看下@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注解和构造方法的调用顺序的更多相关文章

  1. Java类加载及实例化的调用顺序

    标题起得略拗口,大概意思就是说在一个Java类中,域和构造方法的调用顺序. 1. 没有继承的情况 单独一个类的场景下,初始化顺序为依次为 静态数据,继承的基类的构造函数,成员变量,被调用的构造函数. ...

  2. spring中bean的构造函数,Autowired(Value)注入与@PostConstruct调用顺序

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/yyysylvia/article/deta ...

  3. 【spring源码分析】spring和@PostConstruct注解

    @PostConstruct注解好多人以为是Spring提供的.其实是Java自己的注解. Java中该注解的说明:@PostConstruct该注解被用来修饰一个非静态的void()方法.被@Pos ...

  4. Spring@PostConstruct和@PreDestroy注解详解

    @PostConstruct注解使用 @PostConstructApi使用说明 The PostConstruct annotation is used on a method that needs ...

  5. Java学习笔记11---静态成员变量、静态代码块、成员变量及构造方法的初始化或调用顺序

    当创建一个对象时,各种成员变量及构造方法的初始化或调用顺序是怎样的呢? (1).如果类尚未加载,则先初始化静态成员变量和静态代码块,再初始化成员变量,最后调用相应的构造方法: (2).如果类已经加载过 ...

  6. Spring @Cacheable注解 && 事务@Transactional 在同一个类中的方法调用不生效

    @Cacheable 注解在对象内部调用不会生效 代码示例:ProductServiceImpl.java public List<ProductInfoVO> getProductLis ...

  7. @PostConstruct注解原理解析

    所有文章 https://www.cnblogs.com/lay2017/p/11478237.html 正文 @PostConstruct注解使用简介 在了解一个东西的原理之前,我们得初步的懂得如何 ...

  8. 【String注解驱动开发】你了解@PostConstruct注解和@PreDestroy注解吗?

    写在前面 在之前的文章中,我们介绍了如何使用@Bean注解指定初始化和销毁的方法,小伙伴们可以参见<[Spring注解驱动开发]如何使用@Bean注解指定初始化和销毁的方法?看这一篇就够了!!& ...

  9. Spring系列之Spring常用注解总结

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...

随机推荐

  1. DButils分析

    package com.ldf.utils; import java.sql.Connection; public class DBUtils { private static String driv ...

  2. 文件上传(Servlet/Struts2/SpringMVC)

    文件下载(Servlet/Struts2)的链接:http://www.cnblogs.com/ghq120/p/8328093.html 文件上传 Servlet实现 要实现文件上传的功能,必须在f ...

  3. 普通平衡树Tyvj1728、luogu P3369 (treap)

    您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询x数的排名(若有多个相同的数,因输出最小的排名) 查询排名为x的 ...

  4. Java常见异常类

    NullpointException(空指针异常)ClassNotFoundException(类找不到异常)ClassCastException(类型转换异常)IllegalArgumentExce ...

  5. CSS中的onmouseover和hover有什么区别

    它们一样是因为:都是鼠标落上去的时候触发的.它们不一样是因为:onmousemove是javascript里面的,他可以触发js命令,但是hover做不到,hover只是css样式的类,只能定义样式. ...

  6. HTML中的Head标签学习

    在页面加载完成的时候,标签head里的内容,是不会在页面中显示出来的.它包含了像页面的<title>(标题) ,CSS(如果你想用CSS来美化页面内容),图标和其他的元数据(比如 作者,关 ...

  7. js判断是手机还是PC端

    有时接触一些手机上的适应,需要知道是pc 还是移动端 function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = [ ...

  8. 对WebSocket技术的学习与探索(一)

    WebSocket 简要介绍 WebSocket protocol 是HTML5一种新的协议. 它实现了浏览器与服务器全双工通信(full-duple). 一开始的握手需要借助HTTP请求完成. We ...

  9. linux socket中tcp的time_wait的快速回收和重用

    解决方法: 我们可以通过调整内核参数来调整: vi /etc/sysctl.conf 编辑文件,加入以下内容: net.ipv4.tcp_syncookies = net.ipv4.tcp_tw_re ...

  10. [翻译] DXPopover

    DXPopover A Popover mimic Facebook app popover using UIKit. 使用UIKit框架写了一个类似于Facebook的pop效果的动画. The c ...