简体   繁体   English

没有容器也可以使用Spring Framework DI吗?

[英]Is it possible to use Spring Framework DI without a container?

I want to create an java API to connect to a Restful API. 我想创建一个Java API以连接到Restful API。 To deal with dependencies, I want to use Spring. 为了处理依赖关系,我想使用Spring。

However, all examples I see over the internet talks about a DI container, and the whole system is running like a service. 但是,我在互联网上看到的所有示例都谈到了DI容器,并且整个系统都像服务一样运行。

What I need is: Provide some classes to user be able to connect to such Restful API, but the inner dependencies of these classes being resolved by Spring. 我需要的是:为用户提供一些类,使其能够连接到Restful API,但是这些类的内部依赖关系已由Spring解决。 Needless to say that this framework won't start any application server. 不用说,该框架不会启动任何应用程序服务器。 It will just provide some useful classes and interfaces to contact the RestAPI. 它将仅提供一些有用的类和接口来联系RestAPI。

Is that possible? 那可能吗?

Thanks in advance! 提前致谢!

The Situation 情况

Let's suppose I've wrote an API which provides a class called Car. 假设我已经编写了一个API,该API提供了一个名为Car的类。 User can use this Car class in their project. 用户可以在他们的项目中使用此Car类。 But Car class depends on two other classes: Wheel and Engine. 但是Car类取决于其他两个类:Wheel和Engine。 What I want is the last two classes to be injected automatically, without the user needing to instantiate this. 我想要的是最后两个要自动注入的类,而用户无需实例化它。

The short answer to the situation is: no. 对此情况的简短答案是:不。 The reason for this is somebody must start Spring. 原因是有人必须启动Spring。 You have some options. 您有一些选择。

Solution 1 解决方案1

You can create some kind of initialization class that starts Spring for you. 您可以创建某种初始化类来为您启动Spring。 The API user would need to do this: API用户需要执行以下操作:

  1. Call the initialization stuff. 调用初始化的东西。 Possibly something as simple as myAPI.initialize(); 可能像myAPI.initialize();一样简单myAPI.initialize(); .
  2. Then use your stuff. 然后用你的东西。

A giant (imho) drawback of this is that every class in your API would need to check to see if initialization happened. 这样做的一个巨大缺点是,API中的每个类都需要检查初始化是否发生。

Solution 2 解决方案2

Expose your classes via a factory. 通过工厂公开您的课程。 In this case, the API user would do something like this: MyFactory.createCar(...parameters...); 在这种情况下,API用户将执行以下操作: MyFactory.createCar(...parameters...);

With this technique, the Spring initialization would be hidden from the user because you could load Spring in a static initialization block in the MyFactory class. 使用这种技术,Spring初始化将对用户隐藏,因为您可以在MyFactory类的静态初始化块中加载Spring。

Solution 3 解决方案3

Instead of a factory patter, use a builder pattern to instantiate your classes. 可以使用构建器模式来实例化您的类,而不是使用工厂模式。 This is, effectively, a variation of the factory solution, except that each Builder would call a static initialization method of some hidden (perhaps package access) class that starts Spring once. 实际上,这是工厂解决方案的一种变体,除了每个Builder会调用某个隐藏的(可能是程序包访问)类的静态初始化方法(该类初始化后才启动Spring)。

Option other 选项其他

If you only want DI, Spring may not be a good fit for you (it might be overkill). 如果您只想使用DI,Spring可能不适合您(这可能会太过分了)。 Google guice is a "just" DI tool that might be a good fit. Google guice是一个“恰好”的DI工具,可能非常适合。 Even with guice, somebody will still need to start it, so solutions 1-3 will apply. 即使有了guice,还是需要有人启动它,因此适用解决方案1-3。

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

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