当前位置:网站首页>@Postconstruct annotations and initializingbean perform some initialization operations after bean instantiation
@Postconstruct annotations and initializingbean perform some initialization operations after bean instantiation
2022-07-27 12:17:00 【Magic pen code beautiful】
At first it was this code :
@Service
public class userService {
@Autowired
private BaseConfig baseConfig;
private IClient client;
public userService () {
client = ClientFactory.getClient(IClient.class, baseConfig.getBaseUrl());
}
public userinfo getUserInfo(){
return client.getUserInfos();
}
}
As a result, when the program starts running , Found calling in constructor baseConfig The result is baseConfig by null.
Later, we found that ,java stay bean During initialization , Is to execute the constructor of the class first , Instantiate an ordinary object , Then put this ordinary The instance object is stored in singletonFactories Of map in , Subsequent passing inspection Whether circular dependency and APO After operation , Execute initialization of the instance object , Let me put the initialized instance object into the singleton pool .
So in the above operation , By construction method , Get the instance field of dependency injection , It's unrealistic .
resolvent :
One 、 Through annotation @PostConstruct Realize in bean Initialization after instantiation .
@Service
public class userService {
@Autowired
private BaseConfig baseConfig;
private IClient client;
@PostConstruct
public void initClient () {
client = ClientFactory.getClient(IClient.class, baseConfig.getBaseUrl());
}
public userinfo getUserInfo(){
return client.getUserInfos();
}
}
Two 、 By implementing InitializingBean Interface , Realize in bean Initialization after instantiation .
@Service
public class userService implements InitializingBean {
@Autowired
private BaseConfig baseConfig;
private IClient client;
@Override
public void afterPropertiesSet() throws Exception {
client = ClientFactory.getClient(IClient.class, baseConfig.getBaseUrl());
}
public userinfo getUserInfo(){
return client.getUserInfos();
}
}
边栏推荐
- Chapter 8 multithreading
- go入门篇 (5)
- STS下载教程(include官网无法下载解决方案)
- Sword finger offer notes: t57 - ii Continuous positive sequence with sum s
- Idea: can't use subversion command line client: SVN solution
- Chapter 7 exception handling
- shell中的while循环实例
- Do you really understand the underlying data structure skip list of Zset in redis?
- Top 10 in the 5.3 billion Bi Market: fansoft, Microsoft, Yonghong, sap, Baidu, IBM, SAS, smart, salesforce, Inspur soft
- Check the number of file descriptors opened by each process under the system
猜你喜欢

Chapter 7 exception handling

STS下载教程(include官网无法下载解决方案)

B 站 713 事故后的多活容灾建设|TakinTalks 大咖分享

Finding the finite zero point of transfer function under different sampling periods

Shell script text three swordsman awk

Solve the problem of @onetomany query falling into circular reference

npm踩坑

Bishi journey

go 用本地代码replace
![[untitled] multimodal model clip](/img/f0/8ae72ae0845372b6fe2866fae83f52.png)
[untitled] multimodal model clip
随机推荐
基于bolt数据库实现简单的区块链 day(2)
微信小程序必用接口「建议收藏」
Detailed explanation of deeplab series (simple and practical annual summary)
Binary search decision tree (average search length of binary search tree)
Principle, concept and construction process of MySQL database master-slave replication cluster
Shell script text three swordsman awk
Newton Raphson iterative method
npm踩坑
Kazoo tutorial
NPM step pit
解决@OneToMany查询陷入循环引用问题
JS-寄生组合式继承
N ¨UWA: Visual Synthesis Pre-training for Neural visUal World creAtionChenfei
意外收获史诗级分布式资源,从基础到进阶都干货满满,大佬就是强!
Difference quotient approximation of wechat quotient
Sword finger offer notes: t58 - I. flip word order
Leetcode 03: t58. Length of the last word (simple); Sword finger offer 05. replace spaces (simple); Sword finger offer 58 - ii Rotate string left (simple)
MySQL paging query instance_ MySQL paging query example explanation "suggestions collection"
The bank's face recognition system was broken: a depositor was stolen 430000 yuan
Go Introduction (2)