@PostConstruct注解

21 篇文章 0 订阅
21 篇文章 0 订阅

字面解释PostConstruct

将PostConstruct分开成Post 和Construct 两部分

Post解释

在这里插入图片描述
如图所示,post是个介词prep. 意思是 “在…之后”。

Construct 表示构造器

所以@PostConstruct注解就表示“在构造器方法执行之后就执行”

官方定义

@Documented
 @Retention(value=RUNTIME)
 @Target(value=METHOD)
public @interface 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 in a given class can be annotated with this annotation. The method on which the PostConstruct annotation is applied must fulfill all of the following criteria:
The method must not have any parameters except in the case of interceptors in which case it takes an InvocationContext object as defined by the Interceptors specification.
The method defined on an interceptor class or superclass of an interceptor class must have one of the following signatures:
void <METHOD>(InvocationContext)

Object <METHOD>(InvocationContext) throws Exception

Note: A PostConstruct interceptor method must not throw application exceptions, but it may be declared to throw checked exceptions including the java.lang.Exception if the same interceptor method interposes on business or timeout methods in addition to lifecycle events. If a PostConstruct interceptor method returns a value, it is ignored by the container.

The method defined on a non-interceptor class must have the following signature:
void <METHOD>()

The method on which the PostConstruct annotation is applied may be public, protected, package private or private.
The method must not be static except for the application client.
The method should not be final.
If the method throws an unchecked exception the class must not be put into service except in the case where the exception is handled by an interceptor.
Since:
1.6, Common Annotations 1.0
See Also:
PreDestroy, Resource

@PostConstruct注解是是javaEE的注解! javaEE5新增的注解:从Java EE5规范开始,Servlet中增加了两个影响Servlet生命周期的注解,@PostConstruct和@PreDestroy

在这里插入图片描述

野鸡翻译

The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization

被PostConstruct 注解的方法必须在依赖注入完成后才执行以便进行任何的初始化。

The method annotated with PostConstruct must be invoked even if the class does not request any resources to be injected

即是没有任何资源被注入(没有依赖注入),被PostConstruct 注解的方法也必须被调用!

Only one method in a given class can be annotated with this annotation.

一个给定的类中只能有一个方法可以使用@PostConstruct注解。

The method on which the PostConstruct annotation is applied must fulfill all of the following criteria:

方法要使用@PostConstruct 注解必须满足下面所有的条件:

The method must not have any parameters except in the case of interceptors in which case it takes an InvocationContext object as defined by the Interceptors specification.
The method defined on an interceptor class or superclass of an interceptor class must have one of the following signatures:
void <METHOD>(InvocationContext)

Object <METHOD>(InvocationContext) throws Exception

Note: A PostConstruct interceptor method must not throw application exceptions, but it may be declared to throw checked exceptions including the java.lang.Exception if the same interceptor method interposes on business or timeout methods in addition to lifecycle events. If a PostConstruct interceptor method returns a value, it is ignored by the container.

The method defined on a non-interceptor class must have the following signature:
void <METHOD>()

The method on which the PostConstruct annotation is applied may be public, protected, package private or private.
The method must not be static except for the application client.
The method should not be final.
If the method throws an unchecked exception the class must not be put into service except in the case where the exception is handled by an interceptor.

网上资料

参考地址:https://stackoverflow.com/questions/3406555/why-use-postconstruct
在这里插入图片描述
在这里插入图片描述

小结

被@PostConstruct 注解的方法会在构造方法执行完成后并等待依赖注入完成后(如果有依赖注入就等待,没有则不用等待,废话!)执行!

测试demo

新建Springboot项目

在这里插入图片描述

编写@PostConstruct注解的类

package com.xl.test.springtest.postconstruct;

import java.util.Date;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;

import com.xl.test.springtest.service.DeptInfService;

public class MyPostConstruct {
	
//	@Autowired
//	private DeptInfService deptInfService; //依赖注入
	
	@PostConstruct
	public void postMethod() {
		System.out.println(new Date() + " : 正在执行@PostConstruct注解的方法。。。。。。。。。。。。。。。");
	}
	
	public MyPostConstruct() {
		System.out.println(new Date() + " : 构造函数正在执行。。。。。。。。。。。。。。。");
	}
	
}


编写调用类

package com.xl.test.springtest.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xl.test.springtest.postconstruct.MyPostConstruct;

@RestController
public class PostConstructController {
	
	@GetMapping("/test/postconstruct")
	public String testPostConstruct() {
		
		//调用@PostConstruct注解方法所在类的构造方法
		MyPostConstruct myPostConstruct = new MyPostConstruct();
		
		return "aaaaaaa";
	}
}

demo预期:调用@PostConstruct注解方法所在类的构造方法后(这里依赖注入被注释了)后执行被@PostConstruct注解的方法。

启动项目然后调用 http://localhost:8087/test/postconstruct

在这里插入图片描述
在这里插入图片描述
结果显示控制台并没有任何输出,表明@PostConstruct注解的方法并没有执行!

@PostConstruct的执行时机

前面已经提到过,@PostConstruct是Servlet的注解,所以@PostConstruct注解跟Servlet的生命周期有关!

被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。PreDestroy()方法在destroy()方法知性之后执行
在这里插入图片描述
参考文章:https://www.jianshu.com/p/98cf7d8b9ec3

总结:只有在加载Servlet的时候去调用@PostConstruct注解的方法所在类的构造器方法并等待在所在类的依赖注入完成后才会执行@PostConstruct注解的方法

验证

如何在加载Servlet的时候去调用一个类的构造方法

在springboot项目中只需将类标识为组件类(给类加上@Component注解),在项目启动的时候(加载Servlet)spring会自动去实例化所有的组件。

给类MyPostConstruct加上@Component注解如下:

package com.xl.test.springtest.postconstruct;

import java.util.Date;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Component;

@Component
public class MyPostConstruct {
	
//	@Autowired
//	private DeptInfService deptInfService; //依赖注入
	
	@PostConstruct
	public void postMethod() {
		System.out.println(new Date() + " : 正在执行@PostConstruct注解的方法。。。。。。。。。。。。。。。");
	}
	
	public MyPostConstruct() {
		System.out.println(new Date() + " : 构造函数正在执行。。。。。。。。。。。。。。。");
	}
	
}

启动项目查看控制台输出验证结果

在这里插入图片描述

如上图所示,已成功执行@PostConstruct注解的方法!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值