关于@PostConstruct 注解

@PostConstruct注解,好多人以为是Spring提供的。其实是Java自己的注解。

import javax.annotation.PostConstruct;

Java中该注解的说明:@PostConstruct该注解被用来修饰一个非静态的void()方法。被@PostConstruct修饰的方法会在服务器加载Servlet时运行,并且只会被服务器运行一次,类似Servlet的init()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法前运行。

通常我们会在Spring中使用到该注解,该注解的方法在整个Bean初始化中的执行顺序如下:

Constructor(构造方法) -> @Autowired(依赖注入) -> @PostConstruct(注释的方法)

如下代码用于在SpringBoot启动过程中设置系统属性:

package com.ylja.config;

import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;

/**
 * @description: springboot启动时设置cxf运行系统参数
 * @author: wanglp
 * @create: 2020-06-21 22:03
 */
@Component
public class YljaCxfPropertiesConstruct {

    @PostConstruct
    public void doConstruct() throws Exception {
        System.setProperty("org.apache.cxf.stax.allowInsecureParser","1");
    }

}
posted @ 2020-09-16 14:50  漆原Blog  阅读(575)  评论(0)    收藏  举报