英文:
What is the Spring DI equivalent of Dagger2 Subcomponents
问题
Dagger2具有子组件(Subcomponents)功能,用于在比主应用程序更短的生命周期内进行依赖注入(DI)。例如,如果您有一个作业服务(job service),那么该服务将具有一个包含每个作业的子组件的组件(Component)。
在Spring的依赖注入(DI)框架中,相对应的功能是什么?
英文:
Dagger2 has Subcomponents https://medium.com/tompee/dagger-2-scopes-and-subcomponents-d54d58511781 for using DI with shorter lifecycles than the main application, for example if you have a job service then the service will have a Component with Subcomponents for each job.
What is the equivalent for Spring's DI framework?
答案1
得分: 1
以下是翻译好的内容:
我所能看到的唯一可能性就是使用上下文层次结构,并手动处理生命周期较短的上下文。
// Dagger的@Subcomponent.Factory是否起作用
public class JobExecutor {
    private final ApplicationContext mainApplicationContext;
    @Inject
    public JobExecutor(ApplicationContext mainApplicationContext) {
        this.mainApplicationContext = mainApplicationContext;
    }
    public Result execute(Job job) {
        AnnotationConfigApplicationContext jobContext = null;
        try {
            jobContext = new AnnotationConfigApplicationContext();
            jobContext.setParent(mainApplicationContext); // 提供对共享bean的访问
            jobContext.register(SpringJobContext.class);
            jobContext.getBeanFactory().registerSingleton("job", job); // Dagger的@BindsInstance是否起作用
            jobContext.refresh();
            // 您可以手动访问并执行jobContext中的一个bean,或使用@PostConstruct
            return jobContext.getBean(JobExecutorFromJobContext.class)
                    .execute();
        } finally {
            if (jobContext != null) {
                jobContext.destroy();
            }
        }
    }
    static class JobExecutorFromJobContext {
        private final Database database; // 来自父上下文
        private final Job job;
        @Inject
        JobExecutorFromJobContext(Database database, Job job) {
            this.database = database;
            this.job = job;
        }
        public void execute() {
            // ...
        }
    }
}
英文:
The only possibility I can see is to use context hierarchy and to handle context with shorter lifecycle manually.
// Does Dagger's @Subcomponent.Factory work
public class JobExecutor {
    private final ApplicationContext mainApplicationContext;
    @Inject
    public JobExecutor(ApplicationContext mainApplicationContext) {
        this.mainApplicationContext = mainApplicationContext;
    }
    public Result execute(Job job) {
        AnnotationConfigApplicationContext jobContext = null;
        try {
            jobContext = new AnnotationConfigApplicationContext();
            jobContext.setParent(mainApplicationContext); // Provides access to common beans
            jobContext.register(SpringJobContext.class);
            jobContext.getBeanFactory().registerSingleton("job", job); // Does Dagger's @BindsInstance work
            jobContext.refresh();
            // You can access and execute one of the beans from jobContext manually or use @PostConstruct
            return jobContext.getBean(JobExecutorFromJobContext.class)
                    .execute();
        } finally {
            if (jobContext != null) {
                jobContext.destroy();
            }
        }
    }
    static class JobExecutorFromJobContext {
        private final Database database; // From parent's context
        private final Job job;
        @Inject
        JobExecutorFromJobContext(Database database, Job job) {
            this.database = database;
            this.job = job;
        }
        public void execute() {
            // ...
        }
    }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论