Skip to content
鼓励作者:欢迎打赏犒劳

SpringBoot常用技巧

对象实例化之后执行

afterPropertiesSet实现

java
import org.springframework.beans.factory.InitializingBean;

public class MyBean implements InitializingBean {

    private String message;

    public void setMessage(String message) {
        this.message = message;
    }

    public void afterPropertiesSet() throws Exception {
        System.out.println("执行MyBean的初始化方法,message=" + message);
    }
}

PostConstruct实现

java
import javax.annotation.PostConstruct;

public class MyBean {
    
    private String message;

    @PostConstruct
    public void init() {
        System.out.println("执行MyBean的初始化方法,message=" + message);
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

上次更新:

如有转载或 CV 的请标注本站原文地址