当前位置: 首页 > 知识库问答 >
问题:

Spring子上下文关闭父上下文

汪建白
2023-03-14

我有一个使用子/父上下文关系的Spring应用程序。这样做的原因是为了确保子上下文从父上下文继承bean/资源,然后根据需要添加更多bean/资源来覆盖它们。但是,当子上下文关闭时(退出try/catch作用域),它开始对它引用的所有bean进行清理,包括父作用域中的bean。这是不可取的,因为我需要重用父上下文来创建另一个子上下文,但是现在它是垃圾,因为它包含了一堆已处理/关闭的bean。

问题:

>

  • 子上下文应该清理父bean,这是它想要的行为吗?若然,原因为何?

    我希望子上下文只清理它直接定义的bean,而不是继承的bean。这可能吗?

    以下是一些相关代码:

    private AbstractApplicationContext createChildContext(Path workspacePath, String catalogPath, boolean force, Map<String, String> buildOptions) {
            // Set the properties to pass into the new context
            Properties props=new Properties();
            props.setProperty("workspacePath", workspacePath.toString());
            props.setProperty("databasePath", workspacePath.toString() + File.separator + "data");
            props.setProperty("catalog", catalogPath);
            props.setProperty("force",String.valueOf(force));
            PropertiesPropertySource pps=new PropertiesPropertySource("properties",props);
    
            // Create new context
            AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext();
            context.setParent(applicationContext);
            context.getEnvironment().getPropertySources().addFirst(pps);
            context.scan(Neo4jConfig.class.getPackage().getName());
            context.register(Neo4jConfig.class);
    
            ConfigurableListableBeanFactory beans = context.getBeanFactory();
            BuildConfigurationService buildConfiguration = (BuildConfigurationService)beans.createBean(BuildConfigurationService.class);
            buildConfiguration.setBuildConfiguration(buildOptions);
            beans.registerSingleton("buildConfiguration", buildConfiguration);
    
            context.refresh();
    
            return context;
        }
    

    在Neo4jConfig中我们这样做。。。

    @Configuration
    @EnableAutoConfiguration
    @ComponentScan
    @EnableNeo4jRepositories
    @EnableTransactionManagement
    @EnableSpringConfigured
    @EnableCaching(mode=AdviceMode.ASPECTJ)
    @Import({ConversionServiceConfiguration.class})
    public class Neo4jConfig extends Neo4jConfiguration {
    
    ...
    
            @Bean GraphDatabaseService graphDatabaseService(@Value(value = "${databasePath}") String databasePath) {
                    logger.debug("Creating database using '{}' for the database path.",databasePath);
                    GraphDatabaseService graphDB = new GraphDatabaseFactory().newEmbeddedDatabase(databasePath);
        ...
    
  • 共有1个答案

    姜奇
    2023-03-14

    事实证明,问题来自子上下文上的@EnableSpringConfigured注释。这里描述得很模糊。

    当子上下文关闭时,静态的BeanConfigrerSupportbean将被关闭并且不再可用。但是,由于它是静态的(父上下文也使用它),任何未来的@Configurablebean将无法配置

    我希望有人能评论这是一个错误还是期望的行为。

     类似资料:
    • 问题内容: 我有一个Spring bean(dao)对象,该对象通过以下xml在ServletContext中实例化: 该bean在我的webapp-servlet.xml文件中声明,并由我的应用程序在ServletContext中使用。 我也在使用SpringSecurity。据我了解,这在不同的上下文(SecurityContext)中运行。 我的应用程序具有webapp-security.x

    • 我使用spring application builder从父应用程序运行子上下文应用程序(spring Cloud task)。我还将运行的父上下文传递给子应用程序的构建器——它定义了DataSource bean,我希望它也能被task使用。 我的问题是,当关闭子上下文时,所有父bean也会被销毁。我做错什么了吗? 如何将父上下文中的bean与子上下文共享,并且在子上下文退出时仍然保持它们的活

    • 我有一个使用spring hadoop的spring集成项目。我有一个Hbase模板,如下所示, 现在,当我最后关闭应用程序和上下文时,它会抛出一个奇怪的错误, o、 a.h.hbase。客户HConnectionManager:在列表中找不到连接,无法删除它(连接键=HConnectionKey{properties={hbase.zookeer.quorum=xxx.com,xxx.com,h

    • 我将Spring Boot与Spring集成在一起,我希望为每个<code>child() 此时此刻,我正在处理这个:(只有最相关的行) 我已经查看了SpringApplication ationBuilder方法,并且属性从父亲传播到孩子: 但是我需要动态加载一些属性,如下例所示: 从此示例中提取:Spring多个 imapAdapter 这是因为一些Spring集成组件将从配置文件中动态加载。

    • 问题内容: 我在尝试使用定义上下文层次结构时遇到问题。 问题是在内部定义模块上下文并使用另一个上下文(基于XML / Annotated)设置’parent’属性时。 例: 模块A中的beanRefContext.xml db-context.xml 模块B中的beanRefContext.xml 冬眠道 模块B应用程序上下文无法找到在模块A应用程序上下文中定义的bean。 从代码看,扫描过程似乎

    • 在Spring MVC应用程序中,我使用以下方法初始化一个服务类中的变量: UserLibrary是我在应用程序中使用的第三方实用程序。上面的代码为“context”变量生成警告。警告如下所示: 我不明白这个警告。由于应用程序是一个Spring MVC应用程序,当我在应用程序运行时引用服务时,我不能真正关闭/破坏上下文。警告到底想告诉我什么?