先看下@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. bnu 被诅咒的代码

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=10792 被诅咒的代码 Time Limit: 1000ms Memory Limit: 65536K ...

  2. eml文件解析实例,简历信息抓取工具

    先上工具效果图,如下图所示: 背景 某公司使用58同城进行人员招聘,当有应聘人员通过58同城给该公司投简历后,58同城会发送一份邮件到该公司的注册邮箱,邮件内容如上图右侧显示,主题为“应聘贵公司XXX ...

  3. Django,ajax实现表格增删查改,Django内置分页功能。

    1.工程目录 2.urls.py """Django_ajax URL Configuration The `urlpatterns` list routes URLs ...

  4. drupal7 profile2模块获取个人信息

    一.问题背景: 用profile2模块,扩展个人信息,增加了“手机号”等信息,一些地方想要获取当前用户的手机号 二.解决办法: 用profile2自带的方法:profile2_load_by_user ...

  5. .net C# Sql数据库SQLHelper类

    using System;using System.Collections.Generic;using System.Text;using System.Collections;using Syste ...

  6. swiper动态改变滑动内容

    假设当前显示的是1,往左滑动一个递减1,往右滑动一个递增1 body下面添加如下代码 <div class="swiper-container temp"> <d ...

  7. Appium+java移动端项目测试问题整理

    一.每次打开APP都要重新安装.充值账号密码 解决:打开appium,设备,Use Browser  ,勾选“No Reset”   二.一个页面包含相同文字,打开页面路径错误 问题描述:APP处于[ ...

  8. IIS7 使用server farms 进行负载均衡

    1.首先,到微软官网下载web平台安装程序: https://www.microsoft.com/web/downloads/ 2.安装好后,会在IIS里有这个图标: 3.双击这个图标:安装 4.安装 ...

  9. Linux Swap扩容

    最近在做rac,在环境检查的时候发现swap空间检查不通过,所以我们第一想到的是对swap进行扩容,那么swap扩容有哪些方法呢?这里主要介绍两种方法,一通过添加额外磁盘,扩展swap分区,二是通过本 ...

  10. Oracle 检查表的数据变动

    本知识点仅适用于Oracle 9i以上的版本. 查看表的数据变动情况请使用SQL语句:select * from user_tab_modifications; user_tab_modificati ...