什么是Spring DI中与Dagger2的Subcomponents相当的部分?

huangapple go评论141阅读模式
英文:

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

以下是翻译好的内容:

我所能看到的唯一可能性就是使用上下文层次结构,并手动处理生命周期较短的上下文。

  1. // Dagger的@Subcomponent.Factory是否起作用
  2. public class JobExecutor {
  3. private final ApplicationContext mainApplicationContext;
  4. @Inject
  5. public JobExecutor(ApplicationContext mainApplicationContext) {
  6. this.mainApplicationContext = mainApplicationContext;
  7. }
  8. public Result execute(Job job) {
  9. AnnotationConfigApplicationContext jobContext = null;
  10. try {
  11. jobContext = new AnnotationConfigApplicationContext();
  12. jobContext.setParent(mainApplicationContext); // 提供对共享bean的访问
  13. jobContext.register(SpringJobContext.class);
  14. jobContext.getBeanFactory().registerSingleton("job", job); // Dagger的@BindsInstance是否起作用
  15. jobContext.refresh();
  16. // 您可以手动访问并执行jobContext中的一个bean,或使用@PostConstruct
  17. return jobContext.getBean(JobExecutorFromJobContext.class)
  18. .execute();
  19. } finally {
  20. if (jobContext != null) {
  21. jobContext.destroy();
  22. }
  23. }
  24. }
  25. static class JobExecutorFromJobContext {
  26. private final Database database; // 来自父上下文
  27. private final Job job;
  28. @Inject
  29. JobExecutorFromJobContext(Database database, Job job) {
  30. this.database = database;
  31. this.job = job;
  32. }
  33. public void execute() {
  34. // ...
  35. }
  36. }
  37. }
英文:

The only possibility I can see is to use context hierarchy and to handle context with shorter lifecycle manually.

  1. // Does Dagger's @Subcomponent.Factory work
  2. public class JobExecutor {
  3. private final ApplicationContext mainApplicationContext;
  4. @Inject
  5. public JobExecutor(ApplicationContext mainApplicationContext) {
  6. this.mainApplicationContext = mainApplicationContext;
  7. }
  8. public Result execute(Job job) {
  9. AnnotationConfigApplicationContext jobContext = null;
  10. try {
  11. jobContext = new AnnotationConfigApplicationContext();
  12. jobContext.setParent(mainApplicationContext); // Provides access to common beans
  13. jobContext.register(SpringJobContext.class);
  14. jobContext.getBeanFactory().registerSingleton("job", job); // Does Dagger's @BindsInstance work
  15. jobContext.refresh();
  16. // You can access and execute one of the beans from jobContext manually or use @PostConstruct
  17. return jobContext.getBean(JobExecutorFromJobContext.class)
  18. .execute();
  19. } finally {
  20. if (jobContext != null) {
  21. jobContext.destroy();
  22. }
  23. }
  24. }
  25. static class JobExecutorFromJobContext {
  26. private final Database database; // From parent's context
  27. private final Job job;
  28. @Inject
  29. JobExecutorFromJobContext(Database database, Job job) {
  30. this.database = database;
  31. this.job = job;
  32. }
  33. public void execute() {
  34. // ...
  35. }
  36. }
  37. }

huangapple
  • 本文由 发表于 2020年5月4日 22:16:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/61594354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定