当前位置:网站首页>@Detailed explanation of postconstruct annotation
@Detailed explanation of postconstruct annotation
2022-07-29 00:20:00 【Cardiac paranoia】
Initialization mode one :@PostConstruct annotation
Assuming that the class UserController There's a member variable UserService By **@Autowired** modification , that UserService The injection is in UserController After the construction method of .
If you want to UserController Object generation completes some initialization operations , But these initialization operations depend on the injected objects , Then it can't be implemented in the constructor (ps:spring Initialization exception at startup ), for example :
public class UserController {
@Autowired
private UserService userService;
public UserController() {
// call userService Custom initialization method for , here userService by null, Report errors
userService.userServiceInit();
}
}
therefore , have access to @PostConstruct Annotation to complete initialization ,@PostConstruct The method of annotation will be in UserService Automatically called after injection .
public class UserController {
@Autowired
private UserService userService;
public UserController() {
}
// Initialization method
@PostConstruct
public void init(){
userService.userServiceInit();
}
}
** summary :** Class initialization call sequence :
(1) Construction method Constructor
(2)@Autowired
(3)@PostConstruct
Initialization mode two : Realization InitializingBean Interface
In addition to using annotations to complete initialization , It can also be achieved by InitializingBean Complete class initialization
public class UserController implements InitializingBean {
@Autowired
private UserService userService;
public UserController() {
}
// Initialization method
@Override
public void afterPropertiesSet() throws Exception {
userService.userServiceInit();
}
}
The more common ones are SqlSessionFactoryBean, It is achieved through InitializingBean Finished initializing .
@Override
public void afterPropertiesSet() throws Exception {
// buildSqlSessionFactory() Is the core method to complete initialization , Must be executed after the constructor call
this.sqlSessionFactory = buildSqlSessionFactory();
}
边栏推荐
- Solution: direct local.Aar file dependencies are not supported when building an aar
- 1-7 solve the problem of this pointing of methods in classes
- Centos7 install mysql8
- Attack and defense world web master advanced area web_ php_ include
- Samsung asset management (Hong Kong) launched yuancosmos ETF to focus on investing in the future tuyere track
- Locally connect to redis on Windows Server
- Advanced area of attack and defense world web masters ics-06
- JS four formulas for judging data types
- Doip test development practice
- 乱打日志的男孩运气怎么样我不知道,加班肯定很多!
猜你喜欢

Advanced area of attack and defense world web masters unserialize3

Plato farm is expected to further expand its ecosystem through elephant swap

Concurrency in go

Es6操作教程

熊市下PLATO如何通过Elephant Swap,获得溢价收益?

110道 MySQL面试题及答案 (持续更新)

Sword finger offer 55 - I. depth of binary tree

Advanced area of attack and defense world web masters supersqli

Google browser, no installation required

Web系统常见安全漏洞介绍及解决方案-CSRF攻击
随机推荐
PHP语言基础知识(超详细)
递归/回溯刷题(下)
Exchange 2013 SSL certificate installation document
Leetcode60. permutation sequence
Attack and defense world web master advanced area PHP_ rce
Idea connection database
Detailed explanation of the usage of exists in MySQL
Field injection is not recommended solution
"Method not allowed", 405 problem analysis and solution
What is in word?:^ p
MySQL installation and configuration tutorial (super detailed, nanny level)
NPM replace the latest Taobao image
laptop外接显示器
html+css+php+mysql实现注册+登录+修改密码(附完整代码)
Leetcode61. rotating linked list
Feign call fails. JSON parse error illegal character ((ctrl-char, code 31)) only regular white space (R
Develop effective Tao spell
【小程序项目开发 -- 京东商城】uni-app 商品分类页面(上)
动态规划问题(三)
@PostConstruct注解详解