温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot中的Bean初始化方法是什么

发布时间:2021-11-29 13:34:13 来源:亿速云 阅读:327 作者:iii 栏目:开发技术

本篇内容介绍了“SpringBoot中的Bean初始化方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

注解说明

  • 使用注解: @PostConstruct

  • 效果:在Bean初始化之后(构造方法和@Autowired之后)执行指定操作。经常用在将构造方法中的动作延迟。

  • 备注:Bean初始化时候的执行顺序: 构造方法 -> @Autowired -> @PostConstruct

代码示例

注解示例

@Component
public class PostConstructTest1 {
    @Autowired
    PostConstructTest2 postConstructTest2;
    public PostConstructTest1() {
//        postConstructTest2.hello();
    }
    @PostConstruct
    public void init() {
        // some init function
    }
}

在Bean的初始化操作中,有时候会遇到调用其他Bean的时候报空指针错误。这时候就可以将调用另一个Bean的方法这个操作放到@PostConstruct注解的方法中,将其延迟执行。

错误示例

@Component
    public class PostConstructTest1 {
        @Autowired
        PostConstructTest2 postConstructTest2;
        public PostConstructTest1() {
            postConstructTest2.hello();
        }
    }
@Component
    public class PostConstructTest2 {
        public void hello() {
            System.out.println("hello, i am PostConstructTest2");
        }
    }

SpringBoot中的Bean初始化方法是什么

正确示例

@Component
    public class PostConstructTest1 {
        @Autowired
        PostConstructTest2 postConstructTest2;
        public PostConstructTest1() {
            postConstructTest2.hello();
        }
    }
@Component
public class PostConstructTest1 {
    @Autowired
    PostConstructTest2 postConstructTest2;
    public PostConstructTest1() {
//        postConstructTest2.hello();
    }
    @PostConstruct
    public void init() {
        postConstructTest2.hello();
    }
}

SpringBoot中的Bean初始化方法是什么

SpringBoot @PostConstruct虽好,也要慎用

做过SpringBoot开发的话,肯定对@PostConstruct比较熟悉。在一个Bean组件中,标记了@PostConstruct的方法会在Bean构造完成后自动执行方法的逻辑。

1 问题的产生

先说下SpringBoot中Bean的加载过程,简单点说就是SpringBoot会把标记了Bean相关注解(例如@Component、@Service、@Repository等)的类或接口自动初始化全局的单一实例,如果标记了初始化顺序会按照用户标记的顺序,否则按照默认顺序初始化。在初始化的过程中,执行完一个Bean的构造方法后会执行该Bean的@PostConstruct方法(如果有),然后初始化下一个Bean。

那么: 如果@PostConstruct方法内的逻辑处理时间较长,就会增加SpringBoot应用初始化Bean的时间,进而增加应用启动的时间。因为只有在Bean初始化完成后,SpringBoot应用才会打开端口提供服务,所以在此之前,应用不可访问。

2 案例模拟

为了模拟上面说的情况,在SpringBoot项目中建两个组件类ComponentOne和ComponentTwo。耗时的初始化逻辑放在ComponentOne中,并设置ComponentOne的初始化顺序在ComponentTwo之前。完整代码如下:

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ComponentOne {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    public ComponentOne() {
        this.logger.info("ComponentOne 初始化完成");
    }
    @PostConstruct
    public void init() {
        this.logger.info("ComponentOne 模拟耗时逻辑开始");
        try {
        	//这里休眠5秒模拟耗时逻辑
            Thread.sleep(1000 * 5);
        } catch (InterruptedException e) {
            logger.info("模拟逻辑耗时失败", e);
        }
        this.logger.info("ComponentOne 模拟耗时逻辑完成");
    }
}
@Component
@Order(Ordered.HIGHEST_PRECEDENCE + 1)
public class ComponentTwo {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    public ComponentTwo() {
        this.logger.info("ComponentTwo 初始化完成");
    }
    @PostConstruct
    public void init() {
        this.logger.info("ComponentTwo 初始化完成后处理");
    }
}

启动应用,初始化部分日志如下:

SpringBoot中的Bean初始化方法是什么

“SpringBoot中的Bean初始化方法是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI