Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disallow Bean Definition Overriding?

Tags:

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?

like image 903
Larry North Avatar asked Mar 19 '23 06:03

Larry North


1 Answers

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);
    }

}

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

like image 94
Artem Bilan Avatar answered Apr 06 '23 12:04

Artem Bilan