Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing Spring Beans from Application Context [duplicate]

I am using the below to display all spring beans in the context. How can I remove beans from the context?

 ConfigurableApplicationContext ctx = SpringApplication.run(ProxyApplication.class, args);
 for(String beanName:ctx.getBeanDefinitionNames()){
     System.out.println(beanName);
 }
like image 972
Punter Vicky Avatar asked Jan 05 '23 14:01

Punter Vicky


1 Answers

You can do it this way:

    ConfigurableApplicationContext ctx = SpringApplication.run(ProxyApplication.class, args);
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) ctx.getAutowireCapableBeanFactory();
    for(String beanName : ctx.getBeanDefinitionNames()){
        System.out.println(beanName);
        registry.removeBeanDefinition(beanName);
    }
like image 77
Rae Burawes Avatar answered Jan 08 '23 04:01

Rae Burawes