简体   繁体   English

在自己的Java框架中支持多个DI容器

[英]Support multiple DI containers in own Java framework

In my own multi-purpose Java framework, how to use dependency injection without depending on a concrete DI container? 在我自己的多用途Java框架中,如何使用依赖注入而不依赖于具体的DI容器? That is, any application should be able to use my framework, no matter whether it uses CDI, Spring or Guice itself. 也就是说,任何应用程序都应该能够使用我的框架,无论它是使用CDI,Spring还是Guice本身。

All of the following should be possible: 以下所有内容都应该是可能的:

  • for my framework to inject dependencies into the application 我的框架将依赖项注入应用程序
  • for the application to inject dependencies into my framework 为应用程序注入依赖项到我的框架
  • for a component of my framework to inject dependencies into another component 我的框架的一个组件将依赖项注入另一个组件

JSR 330: Dependency Injection for Java is the specification with the largest level of consensus within the dependency injection community and as a result is supported by: JSR 330:Java的依赖注入是依赖注入社区中具有最大共识的规范,因此得到以下支持:

That makes JSR 330 the best common denominator to have portability accross the dependency injection frameworks. 这使得JSR 330成为在依赖注入框架中具有可移植性的最佳共同点。 Which obviously comes at the cost of the range of functionalities. 这显然是以功能范围为代价的。

You'll have to come up with a lowest common denominator interface for all DI operations you need and implementations for each concrete instance you may want to use. 您必须为所需的所有DI操作提供最小公分母接口,并为您可能想要使用的每个具体实例实现。

You can't have any DI engine specific annotations in your objects, so you'll have to externalize all configuration and use setter or constructor injection for all objects. 您的对象中不能有任何特定于DI引擎的注释,因此您必须外部化所有配置并对所有对象使用setter或构造函数注入。

Personally, I think this is a waste of time. 我个人认为这是浪费时间。 DI ought to be a commodity. DI应该是一种商品。 I'd see no good reason for switching DI engines. 我认为切换DI引擎没有充分的理由。 It's more likely that you'll choose one that meets your needs and stick with it. 您更有可能选择满足您需求并坚持下去的产品。

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

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