栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot SmartInitializingSingleton接口

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot SmartInitializingSingleton接口

目录
  • SmartInitializingSingleton
  • 实例

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) () -> {
						smartSingleton.afterSingletonsInstantiated();
						return null;
					}, getAccessControlContext());
				}
				else {
					smartSingleton.afterSingletonsInstantiated();
				}
				smartInitialize.end();
			}
		}
 

该段代码是发生在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 注入成功");
    }
}

输出 :

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号