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修饰

 1 @Component
 2 public class ParentBean implements InitializingBean{
 3     
 4     public ParentBean() {
 5         System.out.println("ParentBean construct");
 6     }
 7     
 8     @PostConstruct
 9     public void init(){
10         System.out.println("ParentBean init");
11     }
12 
13     public void afterPropertiesSet() throws Exception {
14         System.out.println("ParentBean afterPropertiesSet");
15     }
16 
17 }

子类class,也被 @component注解修饰,其余配置和父类class一样

 1 @Component
 2 public class SonBean extends ParentBean{
 3     public SonBean() {
 4         System.out.println("SonBean construct");
 5     }
 6     
 7     @PostConstruct
 8     public void init(){
 9         System.out.println("SonBean init");
10     }
11     @Override
12     public void afterPropertiesSet() throws Exception {
13         System.out.println("SonBean afterPropertiesSet");
14     }
15     
16 }

然后我们使用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

转载于:https://www.cnblogs.com/warehouse/p/6846434.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
@PostConstruct注解的作用是在对象创建完成之后自动调用标记的方法,可以在这个方法中进行一些初始化操作。与构造函数不同,@PostConstruct注解的方法可以使用依赖注入的属性,因此可以在这个方法中对依赖的属性进行一些操作,而不需要在构造函数中手动传入。 使用@PostConstruct注解的方法需要满足以下条件: - 不能有参数 - 不能有返回值 - 不能抛出异常 - 可以是任意访问修饰符的非静态方法 如果一个类中有多个使用@PostConstruct注解的方法,这些方法的执行顺序是不确定的。 在Spring Boot中,@PostConstruct注解的作用与上述相同,用于在Spring容器管理中执行一些初始化操作。它的执行顺序是在ApplicationContextCreated、Bean Registrered、Construct之后,最后执行PostConstruct。 范例:<<引用:@PostConstruct注解的作用和使用。 @PostConstruct注解javax.annotation包下的一个注解,它用于标记一个方法,在对象创建完成,依赖注入完成之后执行。 作用: @PostConstruct注解的方法在对象创建完成之后自动调用,可以在这个方法中进行一些初始化操作。与构造函数不同,@PostConstruct注解的方法可以使用依赖注入的属性,因此可以在这个方法中对依赖的属性进行一些操作,而不需要在构造函数中手动传入。 使用: 在需要使用@PostConstruct注解的方法上添加@PostConstruct注解。该方法不能有参数,不能有返回值,也不能抛出异常。@PostConstruct注解的方法可以是任意访问修饰符,可以是非静态方法。如果一个类中有多个使用@PostConstruct注解的方法,这些方法的执行顺序是不确定的。 。引用:在Spring Boot中,@PostConstruct 注解的作用。在Spring容器管理中,它的执行顺序: ApplicationContextCreated Bean Registrered Construct PostConstruct。>> @PostConstruct注解的作用是在对象创建完成之后自动调用标记的方法,可以在这个方法中进行一些初始化操作。与构造函数不同,@PostConstruct注解的方法可以使用依赖注入的属性,因此可以在这个方法中对依赖的属性进行一些操作,而不需要在构造函数中手动传入。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值