Spring 中 @PostConstruct 注解执行两次的原因及解决方案

spring 中 @postconstruct 注解执行两次的原因及解决方案

本文旨在解释 Spring 框架中 @PostConstruct 注解在某些情况下会被执行两次的原因,并提供相应的解决方案。通常,这种情况是由于创建了多个 Spring 上下文导致的。理解 Spring 上下文的生命周期以及 Bean 的作用域是解决此问题的关键。

理解 Spring 上下文

在 Spring 应用中,ApplicationContext 是一个核心概念,它负责管理 Bean 的生命周期,包括 Bean 的创建、初始化、销毁等。 当我们使用 @PostConstruct 注解时,Spring 会在 Bean 初始化完成后调用被注解的方法。

问题中出现的 @PostConstruct 方法执行两次的根本原因在于创建了两个独立的 Spring 上下文:

主 Spring 上下文: 由 SensitiveWordsApplication 启动时创建和管理。手动创建的 Spring 上下文: 在 TextFilter 类中使用 AnnotationConfigApplicationContext 手动创建。

由于每个 Spring 上下文都独立管理自己的 Bean,因此 MyCache Bean 会在每个上下文中被创建一次,导致 @PostConstruct 方法被调用两次。

解决方案

避免 @PostConstruct 执行两次的关键在于确保只存在一个 Spring 上下文,或者至少确保只在一个上下文中创建和管理 MyCache Bean。 以下是一些常见的解决方案:

1. 移除手动创建的 Spring 上下文

这是最直接的解决方案。 TextFilter 类不需要手动创建 AnnotationConfigApplicationContext。 可以通过 Spring 的依赖注入机制,将 MyCache Bean 注入到 TextFilter 类中。

@Component // 将 TextFilter 纳入 Spring 管理public class TextFilter {    @Autowired // 自动注入 MyCache Bean    private MyCache cache;    public String filter(String originalText) {        return this.cache.get().filter(originalText);    }}

注意事项:

确保 TextFilter 类也被 Spring 管理,可以使用 @Component、@Service、@Repository 或 @Controller 等注解。@Autowired 注解会自动从 Spring 上下文中查找类型匹配的 Bean 并注入。

2. 如果必须使用手动创建的上下文

如果确实需要在 TextFilter 中使用手动创建的 AnnotationConfigApplicationContext,需要确保 MyCache Bean 只在一个上下文中定义。 可以将 MyCache 的定义从 AppConfig 移除,只在手动创建的上下文中定义。

public class TextFilter {    private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();    private MyCache cache;    public TextFilter() {        this.context.scan("com.sensitive_words.utils");        // 在 TextFilter 的上下文中注册 MyCache        this.context.register(MyCache.class);        this.context.refresh();        this.cache = this.context.getBean(MyCache.class);    }    public String filter(String originalText) {        return this.cache.get().filter(originalText);    }}

并且从 AppConfig 中移除 MyCache 的 Bean 定义:

@Configuration@EnableSchedulingpublic class AppConfig {    //  移除 MyCache 的 Bean 定义    // @Bean    // public MyCache myCache() {    //     return new MyCache();    // }}

注意事项:

这种方法比较复杂,不推荐使用,除非有特殊的需求。需要仔细管理 Bean 的作用域,避免出现意外的问题。

3. 使用 ConfigurableBeanFactory.SCOPE_PROTOTYPE

如果需要每次都创建一个新的 MyCache 实例,可以将 Bean 的作用域设置为 prototype。

@Configuration@EnableSchedulingpublic class AppConfig {    @Bean    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)    public MyCache myCache() {        return new MyCache();    }}

注意事项:

prototype 作用域的 Bean 不由 Spring 完全管理生命周期,需要手动销毁。每次从 Spring 上下文中获取 MyCache Bean 时,都会创建一个新的实例,@PostConstruct 方法也会被调用一次。

总结

@PostConstruct 方法被执行多次通常是由于创建了多个 Spring 上下文导致的。 解决此问题的关键在于确保 Bean 只在一个上下文中被创建和管理。 移除手动创建的 Spring 上下文,或者使用 Spring 的依赖注入机制是推荐的解决方案。 在选择解决方案时,需要根据具体的应用场景和需求进行权衡。

以上就是Spring 中 @PostConstruct 注解执行两次的原因及解决方案的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/127626.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年11月27日 18:29:54
下一篇 2025年11月27日 19:00:32

相关推荐

发表回复

登录后才能评论
关注微信