-1

I have 2 dependent applications. When my child application up we update some more beans and refresh the AnnotationconfigwebapplicationContext object, but after refresh the context my MqttConnection object gets started being connected and disconnect.
I don't want to refresh my Mqttconnection object.

Please suggest how I can ignore/remove the MqttConnection object from AnnotationconfigwebapplicationContext before the refresh.


    static{
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.regirster(MqttConnection.class);
    context.register(Config.class);
    
    if(checkChildServiceup()){
      Class<?>[] configClasses = getServletConfigClasses();
      if (!ObjectUtils.isEmpty(configClasses)) {
        this.context.register(configClasses);
      }
      this.context.refresh();
     }
     
     }
      
     
    public static Class<?> getServletConfigClasses(){
    retrun new Class[]{AppConfig.class,devService.class,DbConfiguration.class}; 
    }
    
    public boolean static checkChildServiceup(){
      while(true){
          
    if(up){
    return true;
    break;    
      }
    }

Mario Codes
  • 689
  • 8
  • 15
  • For the next time, please check the code you paste is okay-ish. Your `returns` are written as `retrun` and you have a `break;` after a `return true;`, which makes it unreachable code and does not compile. – Mario Codes Oct 04 '20 at 15:03

1 Answers1

0

During the refresh, the following happens in spring:

  1. It scans all the "resources" of configurations (XMLs, java @Config files, classes annotated with @Component and its derivatives and so forth) And creates Bean Definitions which is a metadata for each bean. Note, bean definition is a meta object, its not the bean object itself.

  2. Calls hooks called "BeanFactoryPostProcessor"-s. These allow to alter the bean definition registry - an object that aggregates all the "information" resolved during step

  3. Initializes beans: creates, autowires, etc.

So you might be required to implement your own BeanFactoryPostProcessor that will check the property set dynamically as I see and will remove the bean definition of MqttConnection from the registry.

@Component // (or register in @Configuration annotated class):
// this must be a spring driven bean by itself, 
// it resolves it but "puts aside" because it 
// recognizes that its a special "hook" 

public class SampleBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

  @Override
  public void postProcessBeanFactory (
            ConfigurableListableBeanFactory beanFactory)
            throws BeansException {

       if(<WHATEVER_PROPERTY_CHANGED__PERFORM_THE CHECK)) {
         ((BeanDefinitionRegistry) beanFactory).removeBeanDefinition("myBean");
      }
  }
}

As for actual removal of the bean definition there are many possible options here, make sure you read this SO thread and also this tutorial for examples.

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • Thanks but some of the objects which are refreshing are using @Autowired Mqtt Connection they are not using old reference hence they are failing. – Rahul Srivastava Oct 03 '20 at 16:27
  • So what would you like to do with this components? You could alter the bean definition to create a no op. Proxy for Mqtt connection or something? Its also possible to use bean post processor for that... – Mark Bramnik Oct 03 '20 at 16:46
  • I saw that you've accepted the answer but then removed that... Have you faced any difficulties with this approach? If you find an alternative solution - consider sharing it for the benefit of community. – Mark Bramnik Oct 05 '20 at 07:21