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;
}
}