有没有办法防止bean覆盖Spring Boot? [英] Is there a way to prevent bean overriding with Spring Boot?

查看:1529
本文介绍了有没有办法防止bean覆盖Spring Boot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Spring的 AbstractRefreshableApplicationContext ,如果Bean ID或循环引用中存在冲突,我可以通过设置一些标志并刷新上下文来强制Spring失败:

With Spring's AbstractRefreshableApplicationContext, I am able to force Spring to fail if there is a conflict in Bean IDs or circular references by setting a couple of flags and refreshing the context like so:

AbstractRefreshableApplicationContext refreshableContext;
...
refreshableContext.setAllowBeanDefinitionOverriding(false);
refreshableContext.setAllowCircularReferences(false);
refreshableContext.refresh();

但是,Spring Boot返回 ConfigurableApplicationContext ,它不是AbstractRefreshableApplicationContext的实例,似乎没有任何方法可以阻止bean定义重写或循环引用。

However, Spring Boot returns a ConfigurableApplicationContext which is not an instance of AbstractRefreshableApplicationContext and does not appear to have any means to prevent bean definition overriding or circular references.

有没有人知道某种方法,并举例说明如何防止这些类型的冲突?

Does anyone know of a way and have an example of how to prevent these types of conflicts?

对于上下文,这适用于具有带注释和xml定义bean的混合的大型项目。使用的Spring Boot版本是1.3.1.RELEASE。在某些情况下,人们在xml中添加了重复的bean定义,但是应用程序启动正常,并且在发生运行时问题之前,原始bean被覆盖并不是很明显。

For context, this is for a large project that has a mix of annotated and xml defined beans. The version of Spring Boot used is 1.3.1.RELEASE. There have been some cases where folks added duplicate bean definitions in the xml, but the application started up fine and wasn't immediately apparent the original bean was overridden until run-time issues started occurring.

此处的目标是在发生此类冲突时阻止应用程序启动事件。从各种论坛我知道Spring IDE可以检测到这些,但是希望在CI构建中强制执行这个,这是一个更强大的安全网。

The goal here is to prevent the application from event starting up if such a conflict occurs. From various forums I know Spring IDE can detect these, but the desire is to enforce this in the CI build which is a stronger safety net.

经过一些搜索,我可以在Sprint Boot返回的上下文中找不到任何支持。如果无法通过上下文完成此操作,是否有不同的解决方案?

After some searching, I can't find any support for this in the context that Sprint Boot returns. If this can't be done through the context, is there a different solution available?

提前致谢。

推荐答案

在构建Spring Boot应用程序时可以使用初始化程序:

You may use an initializer when building your Spring Boot app:

@SpringBootApplication
public class SpringBootApp {

    public static void main(String... args) {
        new SpringApplicationBuilder(SpringBootApp.class)
            .initializers(new ApplicationContextInitializer<GenericApplicationContext>() {
                @Override
                public void initialize(GenericApplicationContext applicationContext) {
                    applicationContext.setAllowBeanDefinitionOverriding(false);
                }
            })
        .run(args);

    }
}

或者使用java 8:

Or with java 8:

new SpringApplicationBuilder(SpringBootApp.class)
    .initializers((GenericApplicationContext c) -> c.setAllowBeanDefinitionOverriding(false) )
    .run(args);

这篇关于有没有办法防止bean覆盖Spring Boot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆