当前位置:网站首页>@PostConstruct注解详解
@PostConstruct注解详解
2022-07-28 22:23:00 【心动的偏执】
初始化方式一:@PostConstruct注解
假设类UserController有个成员变量UserService被**@Autowired**修饰,那么UserService的注入是在UserController的构造方法之后执行的。
如果想在UserController对象生成时候完成某些初始化操作,而偏偏这些初始化操作又依赖于依赖注入的对象,那么就无法在构造函数中实现(ps:spring启动时初始化异常),例如:
public class UserController {
@Autowired
private UserService userService;
public UserController() {
// 调用userService的自定义初始化方法,此时userService为null,报错
userService.userServiceInit();
}
}
因此,可以使用@PostConstruct注解来完成初始化,@PostConstruct注解的方法将会在UserService注入完成后被自动调用。
public class UserController {
@Autowired
private UserService userService;
public UserController() {
}
// 初始化方法
@PostConstruct
public void init(){
userService.userServiceInit();
}
}
**总结:**类初始化调用顺序:
(1)构造方法Constructor
(2)@Autowired
(3)@PostConstruct
初始化方式二:实现InitializingBean接口
除了采用注解完成初始化,也可以通过实现InitializingBean完成类的初始化
public class UserController implements InitializingBean {
@Autowired
private UserService userService;
public UserController() {
}
// 初始化方法
@Override
public void afterPropertiesSet() throws Exception {
userService.userServiceInit();
}
}
比较常见的如SqlSessionFactoryBean,它就是通过实现InitializingBean完成初始化的。
@Override
public void afterPropertiesSet() throws Exception {
// buildSqlSessionFactory()是完成初始化的核心方法,必须在构造方法调用后执行
this.sqlSessionFactory = buildSqlSessionFactory();
}
边栏推荐
- html+css+php+mysql实现注册+登录+修改密码(附完整代码)
- Android studio连接MySQL并完成简单的登录注册功能
- Advanced area of attack and defense world web masters ics-06
- AutoCAD -- import excel tables into CAD and merge CAD
- 动态规划问题(五)
- ES6 operation tutorial
- SQL实现将多行记录合并成一行
- 110道 MySQL面试题及答案 (持续更新)
- 动态规划问题(二)
- curl (7) Failed connect to localhost8080; Connection refused
猜你喜欢

Detailed principle explanation and verification results of digital clock based on FPGA

跳表的原理

Introduction and solution of common security vulnerabilities in web system CSRF attack

Leetcode61. rotating linked list

Interpretation of ISO 13400 (doip) standard

Immutable x officially opens IMX token pledge detailed IMX pledge introduction optimistic about the development prospect of IMX

ACM SIGIR 2022 | interpretation of selected papers of meituan technical team

Sword finger offer 64. find 1+2+... +n, logical operator short circuit effect

Yolov5 learning notes (I) -- principle overview

centos7安装mysql8
随机推荐
动态规划问题(一)
Develop effective Tao spell
feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
Immutable x officially opens IMX token pledge detailed IMX pledge introduction optimistic about the development prospect of IMX
Android studio connects to MySQL and completes simple login and registration functions
Applet editor rich text editing and rich text parsing
Review of categories 1-4
JS高级 之 ES6~ES13 新特性
mysql索引失效的常见9种原因详解
Type 1-5 components
Event extraction and documentation (2008-2017)
The difference between {} and ${}
Opencv macro definition
【小程序项目开发 -- 京东商城】uni-app 商品分类页面(上)
Erc20 Standard Code
PHP poster QR code synthesis
Leetcode61. rotating linked list
熊市下PLATO如何通过Elephant Swap,获得溢价收益?
With this, your messages can't be monitored
Multimodal model sketch (1)