禁止 Bean 定义覆盖? [英] Disallow Bean Definition Overriding?

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

问题描述

我想禁止在 SpringApplication 中覆盖 bean 定义.换句话说,我想要调用 GenericApplicationContext.setAllowBeanDefinitionOverriding(false) 的效果.在 Spring Boot 中是否有推荐的方法来做到这一点?

I want to disallow bean definition overriding in a SpringApplication. In other words, I want the effect of invoking GenericApplicationContext.setAllowBeanDefinitionOverriding(false). Is there a recommended way for doing that in Spring Boot?

推荐答案

好吧,您可以通过一些 Boot 功能和上下文自定义来实现:

Well, you can achieve it with some Boot features and context customization:

@EnableAutoConfiguration
public class MyApp {

    public static void main(String[] args) throws InterruptedException {
        ConfigurableApplicationContext ctx = new SpringApplicationBuilder(MyApp.class)
                .initializers(new ApplicationContextInitializer<GenericApplicationContext>() {
                    @Override
                    public void initialize(GenericApplicationContext applicationContext) {
                        applicationContext.setAllowBeanDefinitionOverriding(false);
                    }
                }).run(args);
    }

}

您可以从所提到类的源代码和 JavaDocs 中找到的所有其他信息.

All other info you can find from source code and JavaDocs of mentioned classes.

这篇关于禁止 Bean 定义覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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