简体   繁体   English

无法在 Spring 引导应用程序中刷新现有的 singleton bean

[英]Unable to refresh existing singleton bean in Spring boot application

I have a configuration defined in my spring boot applocation as follows:我在我的 spring 引导应用程序中定义了一个配置,如下所示:

@Configuration
public class RuleEngineConfiguration {

  private final DatabaseRuleLoader databaseRuleLoader;

  public RuleEngineConfiguration(
      DatabaseRuleLoader databaseRuleLoader) {
    this.databaseRuleLoader = databaseRuleLoader;
  }

  @Bean
  public RuleEngineManager ruleEngine() {
    return RuleEngineManagerFactory.getRuleEngineManager(this.databaseRuleLoader);
  }

Now I would like to refresh RuleEngineManager bean in my spring boot application on creating/update of a row in a given table in DB with refresh function as defined below:现在我想在我的 spring 引导应用程序中刷新RuleEngineManager bean,以创建/更新数据库中给定表中的一行,刷新 function 如下定义:

public void refresh() {
    databaseRuleLoader.refresh(); <---THIS RELOADS ROWS FROM DB
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) applicationContext
        .getAutowireCapableBeanFactory();
    RuleEngineManager ruleEngineManager = RuleEngineManagerFactory
        .getRuleEngineManager(databaseRuleLoader);
    registry.removeBeanDefinition("ruleEngine");
    ((SingletonBeanRegistry) registry).registerSingleton("ruleEngine", ruleEngineManager);
  }

And in my application, where I need RuleEngineManager bean, I am getting the bean as follows:在我的应用程序中,我需要RuleEngineManager bean,我按如下方式获取 bean:

((RuleEngineManager) applicationContext.getBean("ruleEngine"))

Even though the refresh function is getting executed every time, I am creating/updating any rows in DB, but, I am not seeing any changes.即使每次都执行刷新 function,我正在创建/更新数据库中的任何行,但是,我没有看到任何更改。 It seems the existing RuleEnginemanager bean is getting injected as a dependency.现有的RuleEnginemanager bean 似乎作为依赖项被注入。 I am not able to figure out what I am missing here.我无法弄清楚我在这里缺少什么。

Could anyone please help here?有人可以在这里帮忙吗? Thanks.谢谢。

What I was suggesting is to use a factory (design pattern)... for example in this way:我的建议是使用工厂(设计模式)......例如以这种方式:

// you already have this class
public class RuleEngineManagerFactory{ 
   private DatabaseRuleLoader databaseRuleLoader;
   private RuleEngineManager ruleEngineManager;
   public RuleEngineManagerFactory(DatabaseRuleLoader databaseRuleLoader) {
     this.databaseRuleLoader = databaseRuleLoader;
   }
   public RuleEngineManager getRuleEngineManager(){
     if(this.ruleEngineManager == null){
         ruleEngineManager = new RuleEngineManager(this.databaseRuleLoader);
     }
     return this.ruleEngineManager;
   }
   public void refresh(){
      ruleEngineManager = new RuleEngineManager(this.databaseRuleLoader);
   }
}

In this way, you will inject RuleEngineManagerFactory wherever you need, and you can refresh the RuleEngineManager ... so you will have to have a singleton for the factory of the manager, and not for the manager itself这样,您将在需要的任何地方注入RuleEngineManagerFactory ,并且可以刷新RuleEngineManager ...因此您必须为经理的工厂提供一个 singleton,而不是经理本身

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM