java—如何使用在不同配置类中定义的作用域?

g52tjvyc  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(192)

客户端配置:

@Configuration
public class ClientConfig {
    @Bean
    public DeleteFiles deleteFiles() {
         return new DeleteFiles();
     }

    @Bean
    public RandomClass1 fn1(){
      return new RandomClass1(deleteFiles());
    }
}

用户配置:

@Configuration
public class UserConfig {

    @Autowired
    private DeleteFiles deleteFiles;
    @Autowired
    private RandomClass1 randomClass1;

    @Bean
    public static CustomScopeConfigurer registerWorkflowScope() {
        CustomScopeConfigurer customScopeConfigurer = new CustomScopeConfigurer();
        customScopeConfigurer.addScope("workflow", new WorkflowScope());
        return customScopeConfigurer;
    }

    @Bean
    @Scope("workflow")
    public RandomClass2 fn2() {
        return new RandomClass2(deleteFiles);
    }

    @Bean
    public RandomClass3 fn3(){
        return new RandomClass3(randomClass1);
    }
}

我的用例是用customscope(“工作流”)定义deletefiles,即对于每个新工作流,我应该获得一个新对象。
查询:
我们可以使用userconfig中定义的自定义范围在clientconfig中使用吗。这里是否有任何机制来验证deletefiles的作用域?

@Configuration
public class ClientConfig {
    @Bean
    @Scope("workflow")
    public DeleteFiles deleteFiles() {
         return new DeleteFiles();
     }
    @Bean
    public RandomClass1 fn1(){
      return new RandomClass1(deleteFiles());
    }
}

为了实现上述用例,我尝试在userconfig中定义“deletefiles”,如下所示:
客户端配置:

@Configuration
public class ClientConfig {
    @Autowired
    private DeleteFiles deleteFiles;

    @Bean
    public RandomClass1 fn1(){
      return new RandomClass1(deleteFiles);
    }
}

用户配置:

@Configuration
public class UserConfig {

    @Autowired
    private RandomClass1 randomClass1;

    @Bean
    public static CustomScopeConfigurer registerWorkflowScope() {
        CustomScopeConfigurer customScopeConfigurer = new CustomScopeConfigurer();
        customScopeConfigurer.addScope("workflow", new WorkflowScope());
        return customScopeConfigurer;
    }

    @Bean
    @Scope("workflow")
    public DeleteFiles deleteFiles() {
         return new DeleteFiles();
    }

    @Bean
    @Scope("workflow")
    public RandomClass2 fn2() {
        return new RandomClass2(deleteFiles());
    }

    @Bean
    public RandomClass3 fn3(){
        return new RandomClass3(randomClass1);
    }
}

但当我运行应用程序时,它被卡住了,而且没有抛出任何异常。我想它已经陷入僵局了。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题