提示:本文的代码环境为SpringBoot2.3.4
文章目录
前言
Spring已成为java开发web应用必会技术栈,本系列文章着重为大家揭秘spring中的技术点,本文重点介绍Bean实例化之后,依靠注解PostConstruct可以做的一些初始化的工作。
一、PostConstruct是什么?
从注解的全限定名(javax.annotation.PostConstruct)可以看出这个注解是JDK本身自带的注解,JDK中对该注解有如下的说明
/**
* 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:
* <p>
* <ul>
* <li>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.</li>
* <li>The method defined on an interceptor class MUST HAVE one of the
* following signatures:
* <p>
* void <METHOD>(InvocationContext)
* <p>
* Object <METHOD>(InvocationContext) throws Exception
* <p>
* <i>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.</i>
* </li>
* <li>The method defined on a non-interceptor class MUST HAVE the
* following signature:
* <p>
* void <METHOD>()
* </li>
* <li>The method on which PostConstruct is applied MAY be public, protected,
* package private or private.</li>
* <li>The method MUST NOT be static except for the application client.</li>
* <li>The method MAY be final.</li>
* <li>If the method throws an unchecked exception the class MUST NOT be put into
* service except in the case of EJBs where the EJB can handle exceptions and
* even recover from them.</li></ul>
* @since Common Annotations 1.0
* @see javax.annotation.PreDestroy
* @see javax.annotation.Resource
*/
翻译成中文的意思就是:
PostConstruct注解作用的方法在在完成依赖注入后会被执行,
用来实现任何初始化操作。该方法必须在这个类的实例对象使用
前被执行。所有支持依赖注入的类都必须支持该注解,即使使用
了该注解的类,不要任何资源的注入,该注解标注的方法也必须
被执行,每个类中只有一个方法可以标注这个注解。使用这个注
解的方法必须完全满足以下的要求:
1、方法不能有任何参数除了在拦截器中定义的方法
2、拦截器中定义的方法必须要使用InvocationContext作为参数
3、非拦截器中定义的方法必须为无参,无返回值
4、给注解可以标注在 public, protected,
无修饰符, private 修饰的方法
5、方法不能被static修饰除非是客户端应用
6、可是final修饰的方法
7、如果方法抛出了一个不受检(运行时)异常,这个类将不能被使
用,除非是一个EJB项目,EJB可以处理异常,或者修复他们。
二、可以做什么
从上面的英文注释中我们可以发现PostConstruct的作用就是在依赖注入实力化完成后,可以做一些初始化的工作。
下面举个简单的例子:
先看一下配置类,本文中所有的例子都是使用该配置类
@ComponentScan("com.it.sun.course")
@Configuration
public class CommonConfiguration {
@Bean()
public HelloService helloService() {
return new HelloService();
}
}
服务类的代码如下:
@Slf4j
public class HelloService{
@Autowired
private WorldService worldService;
public HelloService() {
System.err.println("initialize HelloService");
}
@PostConstruct
public void postConstruct() {
System.out.println(worldService);
System.err.println("PostConstruct 注解");
}
}
启动类
public class CourseApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext =
new AnnotationConfigApplicationContext(CommonConfiguration.