java 使用 Spring 进行运行时依赖注入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1439839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:32:50  来源:igfitidea点击:

Runtime dependency injection with Spring

javaspringcommanddesign-patterns

提问by Jimm

My current project is leveraging Spring, and our architect has decided to let Spring manage Services, Repositories and Factory objects, but NOT domain objects. We are closely following domain driven design. The reasoning behind not using spring for domain objects is primarily that spring only allows static dependency injection. What i mean by static dependency injection is that dependencies are specified inside xml configuration and they get "frozen".

我当前的项目是利用 Spring,我们的架构师决定让 Spring 管理服务、存储库和工厂对象,而不是域对象。我们密切关注领域驱动设计。不对域对象使用 spring 的原因主要是 spring 只允许静态依赖注入。我所说的静态依赖注入的意思是依赖在 xml 配置中指定并且它们被“冻结”。

I maybe wrong, but my current understanding is that even though my domain only leverages interfaces to communicate with objects, but spring's xml configuration forces me to specify a concrete dependency. hence all the concrete dependencies have to be resolved at deployment time. Sometimes, this is not feasible. Most of our usecases are based on injecting a particular type based on the runtime data or a message received from an end user.

我可能错了,但我目前的理解是,尽管我的域只利用接口与对象进行通信,但 spring 的 xml 配置迫使我指定具体的依赖项。因此,必须在部署时解决所有具体的依赖关系。有时,这是不可行的。我们的大多数用例都基于根据运行时数据或从最终用户收到的消息注入特定类型。

Most of our design is following command pattern. hence, when we recieve a command, we would like to construct our domain model and based on data received from a command, we inject particular set of types into our aggregate root object. Hence, due to lack of spring's ability to construct a domain model based on runtime data, we are forced to use static factory methods, builders and Factory patterns.

我们的大部分设计都遵循命令模式。因此,当我们收到一个命令时,我们想构建我们的域模型,并基于从命令接收到的数据,我们将特定的类型集注入到我们的聚合根对象中。因此,由于 spring 缺乏基于运行时数据构建域模型的能力,我们被迫使用静态工厂方法、构建器和工厂模式。

Can someone please advise if spring has a problem to the above scenario ?

有人可以建议 spring 对上述情况是否有问题吗?

I could use AOP to inject dependencies, but then i am not leveraging spring's infrastructure.

我可以使用 AOP 来注入依赖项,但是我没有利用 spring 的基础设施。

回答by skaffman

I suggest you read the section in the Spring docs concerning Using AspectJ to dependency inject domain objects with Spring.

我建议您阅读 Spring 文档中有关Using AspectJ to dependency injection domain objects with Spring 的部分

It's interesting that you said "I could use AOP to inject dependencies, but then i am not leveraging spring's infrastructure, " considering that AOP is a core part ofSpring's infrastructure. The two go very well together.

有趣的是,你说:“我可以使用AOP注入依赖,但后来我没有充分利用Spring的基础设施,”考虑到AOP是一个核心部分Spring的基础设施。两人配合得非常好。

The above link allows you to have Spring's AOP transparently inject dependencies into domain objects that are creating without direct reference to the Spring infrastructure (e.g. using the newoperator). It's very clever, but does require some deep-level classloading tinkering.

上面的链接允许您让 Spring 的 AOP 透明地将依赖项注入正在创建的域对象中,而无需直接引用 Spring 基础设施(例如,使用new操作符)。它非常聪明,但确实需要一些深层次的类加载修补。

回答by Nat

Spring's dependency injection/configuration is only meant for configuring low-level technical infrastructure, such as data sources, transaction management, remoting, servlet mount-points and so forth.

Spring 的依赖注入/配置仅用于配置底层技术基础设施,例如数据源、事务管理、远程处理、servlet 挂载点等。

You use spring to route between technical APIs and your services, and inside those services you just write normal Java code. Keeping Spring away from your domain model and service implementations is a good thing. For a start , you don't want to tie your application's business logic to one framework or let low-level technical issues "leak" into your application domain model. Java code is much easier to modify in the IDE than spring's XML config, so keeping business logic in java let's you deliver new features more rapidly and maintain the application more easily. Java is much more expressive than spring's XML format so you can more clearly model domain concepts if you stick to plain Java.

您使用 spring 在技术 API 和您的服务之间进行路由,并且在这些服务中您只需编写普通的 Java 代码。让 Spring 远离域模型和服务实现是一件好事。首先,您不希望将应用程序的业务逻辑绑定到一个框架或让低级技术问题“泄漏”到您的应用程序域模型中。Java 代码在 IDE 中比在 Spring 的 XML 配置中更容易修改,因此将业务逻辑保留在 Java 中可以让您更快地交付新功能并更轻松地维护应用程序。Java 比 Spring 的 XML 格式更具表现力,因此如果您坚持使用纯 Java,您可以更清晰地建模领域概念。

回答by ColinD

Spring's dependency injection (and dependency injection in general) is basically for wiring together Services, Repositories and Factories, etc. It's not supposed to directly handle things that need to be done dynamically in response to commands, etc., which includes most stuff with domain objects. Instead, it provides control over how those things are done by allowing you to wire in the objects you want to use to do them.

Spring 的依赖注入(以及一般的依赖注入)基本上是用于将服务、存储库和工厂等连接在一起。它不应该直接处理需要响应命令等动态完成的事情,其中​​包括大多数域的东西对象。相反,它通过允许您连接要用于执行这些操作的对象来提供对如何完成这些操作的控制。