简体   繁体   English

Spring 使用运行时值初始化上下文

[英]Spring context initialization with runtime values

I'm trying to integrate Spring in a standalone Swing application.我正在尝试将 Spring 集成到独立的 Swing 应用程序中。
The Swing application asks for login details at start-up, which should then be used to create a singleton DataSource Bean. Swing 应用程序在启动时询问登录详细信息,然后应使用该详细信息创建 singleton DataSource Bean。

However I can't come up with a way to pass those login info (as Java object) to the Spring ApplicationContext during initialization (which would then be passed down to the @Bean producer method).但是,我无法想出在初始化期间将这些登录信息(作为 Java 对象)传递给 Spring ApplicationContext的方法(然后将其传递给@Bean生产者方法)。

Any ideas?有任何想法吗?


Possible solution:可能的解决方案:

@SpringBootApplication
public class DemoSwingApplication {
  public static void main(final String[] args) {
    ...

    final var loginInfo = buildLoginInfo();

    try (final var context = new AnnotationConfigApplicationContext()) {
      context.getBeanFactory().registerSingleton("loginInfo", loginInfo);
      context.register(DemoSwingApplication.class);
      context.refresh();
    }
  }
}

There are multiple ways in which you can do this,有多种方法可以做到这一点,

Using BeanDefinitionRegistryPostProcessor - Create a bean which will implement BeanDefinitionRegistryPostProcessor and then store the BeanDefinitionRegistry instance and dynamically register your bean.使用 BeanDefinitionRegistryPostProcessor - 创建一个将实现BeanDefinitionRegistryPostProcessor的 bean,然后存储BeanDefinitionRegistry实例并动态注册您的 bean。

@Component
public class DbConfigurer implements BeanDefinitionRegistryPostProcessor, BeanFactoryAware {

    private BeanDefinitionRegistry beanDefinitionRegistry;

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        this.beanDefinitionRegistry = beanDefinitionRegistry;
    }

    public void registerDataSourceBean() {
        beanDefinitionRegistry.registerBeanDefinition("dataSource", new RootBeanDefinition(DataSource.class,
                BeanDefinition.SCOPE_SINGLETON, yourDataSourceBeanSupplier));
    }
}

Using BeanFactoryAware - This is similar to implementation that you provided but by implementing BeanFactoryAware interface but downside of this is to check for BeanFactory instance -使用 BeanFactoryAware - 这类似于您提供的实现,但通过实现BeanFactoryAware接口,但这样做的缺点是检查BeanFactory实例 -

@Component
public class DbConfigurer implements BeanDefinitionRegistryPostProcessor, BeanFactoryAware {

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) beanFactory; // Need to cast
    }
}

And then in your UI component, inject this And and register bean when config properties are available -然后在你的UI组件中,注入这个And并在配置属性可用时注册 bean -

@Component
public class MainWindow extends JFrame {

    private final DbConfigurer dbConfigurer;

    // register bean once user provides config properties
}

and start your application using headless mode disabled -并使用禁用的无头模式启动您的应用程序 -

@SpringBootApplication
public class DesktopApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(DesktopApplication.class).headless(false).run(args);
    }

}

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

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