- SmartInitializingSingleton
- 实例
// Trigger post-initialization callback for all applicable beans...
// 触发所有适用bean的初始化后回调
for (String beanName : beanNames) {
Object singletonInstance = getSingleton(beanName);
if (singletonInstance instanceof SmartInitializingSingleton) {
StartupStep smartInitialize = this.getApplicationStartup().start("spring.beans.smart-initialize")
.tag("beanName", beanName);
SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction
该段代码是发生在bean对象创建完成之后, 所以如果bean实现了SmartInitializingSingleton接口, 那么就会默认回调afterSingletonsInstantiated()方法, 可以在该方法中对bean进行一些修改
实例@Service
public class UserServiceImpl implements UserService, SmartInitializingSingleton {
@Autowired
private UserMapper userMapper;
@Override
public UserDO selectById(Long id) {
return userMapper.selectById(id);
}
@Override
public List selectBySex(String sex) {
return userMapper.selectBySex(sex);
}
@Override
public List selectByCondition(String name, Integer age) {
return userMapper.selectByCondition(name,age);
}
@Override
public UserDO selectByMap(UserDO user) {
return userMapper.selectByMap(user);
}
@Override
public void afterSingletonsInstantiated() {
System.out.println("UserServiceImpl 注入成功");
}
}
输出 :



