工厂类与 Spring DI

发布于 2024-12-01 16:36:57 字数 345 浏览 0 评论 0原文

据我了解,Factory 类和 Spring DI 都遵循依赖注入。我的意思是在这两种情况下都使用外部实体来推动依赖关系。 对吗? 我的问题是,当我的目的只是获取对象时,我应该在工厂类和 Spring DI 之间选择哪一个。假设我不需要任何其他功能,如 aop、dao 支持等。唯一的目的是从 Factory 类或 Spring DI 获取对象。哪一个更可取。

在某些网站上阅读此声明

DI 与工厂类相比是松散耦合且侵入性较小

但无法了解 spring DI 与工厂类相比如何松散耦合且侵入性较小? 在这两种情况下,我们都必须在核心程序中插入某种获取目标代码。

As per my understanding both Factory class and Spring DI follows the Dependency injection. I mean in both the cases external entity is used to push the dependency. Right?
My question is which one i should go for between factory classes and Spring DI when my intention is just to get the objects . Assume i don't want any other features like aop, dao support etc. Only purpose is to get the objects either from Factory class or Spring DI. Which one is preferable.

on some site read this statement

DI loosely coupled and less intrusive in comparison to Factory classes

But could not get how spring DI loosely coupled and less intrusive than factory classes?
in both the cases we have to insert some kind of get object code in our core program .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤凫 2024-12-08 16:36:57

Spring DI 提倡松散耦合的代码,因为 Spring 容器根据配置注入依赖项。如果您要注入接口实现,则不必更改代码来更改注入的特定实现,除非您考虑配置代码(许多人都会这么做)。

如果您使用工厂来创建由其余代码使用的配置对象,那么您正在编写代码来创建对象、配置它们等。如果您想更改工厂返回的内容,则必须更改实际代码,有些人认为这是一个更具侵入性的变化。

通常,Spring 用于配置应用程序的各个层如何连接在一起。例如,X 服务采用这样那样的 DAO 实现。这就是应用程序级别的组织。假设您有一个场景,想要为列表中的每一行创建一个按钮——在这种情况下,您可以使用工厂来创建按钮。此场景基于运行时情况,其中 GUI 具有您无法预先配置的不同元素(因为它基于数据),因此 DI 在这里意义不大。

编辑 - 根据您的评论问题,我认为这里的要点是您必须考虑的是 Spring 也是一个控制反转容器。这意味着您无需对应用程序中的哪些组件去往何处进行编程。如果没有 IoC,您可能会执行类似的操作,

MyServiceImpl extends MyService {
    Dao1 = new Dao1Impl(); // you programmatically configure which components go in here
    Dao2 = new Dao2Impl();
    ....
}

而不是执行类似的操作。

MyServiceImpl extends MyService {
    public Dao1;  // you haven't specified which components, only interfaces
    public Dao2;
    ....
}

在第二个代码示例中,Spring(或您使用的任何内容)将为您注入适当的 DAO 实例。您已将使用哪些组件的控制权移至更高级别。因此,IoC 和 DI 齐头并进,IoC 促进松散耦合,因为在组件定义(即接口)中,您只指定行为。

换句话说,IoC 和 DI 对于松耦合来说并不是必需的;您也可以与工厂松散耦合,

MyServiceImpl extends MyService {
    public dao1 
    public dao2;

    MyServiceImpl(){
       dao1 = DaoFactory.getDao1();
       ...
    }
    ....
}

在这里您的服务仍然仅依赖于 DAO 定义,并且您使用工厂来获取实现。需要注意的是,您的服务现在已与工厂耦合。如果需要,您可以通过将 Factory 传递到构造函数中来使其更加宽松......

另外,不要忘记 Spring 提供其他有用的功能,例如事务管理。这非常有帮助,即使您说您的应用程序不需要它。

Spring DI promotes loosely coupled code because the Spring container injects your dependencies based on configuration. If you are injecting interface implementations, you don't have to change code to change which specific implementation gets injected, unless you consider your configuration code, which many do.

If you use a Factory to create configured objects that are used by the rest of your code, you are writing code to create the objects, configure them, etc. If you want to change what the factory returns, you have to change actual code, which some would argue is a more intrusive change.

Typically Spring is used to configure how the various layers of your application are wired together. X service takes such and such DAO implementations, for example. That's application level organization. Lets say you have a scenario where want to create a button for every row in a list -- in that case you could use a factory to create the buttons. This scenario is based on a runtime situation where the GUI has different elements that you couldn't configure up front (because its based on the data), so DI makes less sense here.

EDIT - based on your comment questions, I think the primary point here is that you have to consider is that Spring is also an Inversion of Control container. That means you don't program in which components in your application go where. Without IoC, you might do something like

MyServiceImpl extends MyService {
    Dao1 = new Dao1Impl(); // you programmatically configure which components go in here
    Dao2 = new Dao2Impl();
    ....
}

instead you do something like

MyServiceImpl extends MyService {
    public Dao1;  // you haven't specified which components, only interfaces
    public Dao2;
    ....
}

In the second code sample, Spring (or whatever you use) will inject the appropriate DAO instances for you. You have moved control of which components to use to a higher level. So IoC and DI go hand and hand, IoC promotes loose coupling because in your component definitions (i.e. interfaces) you only specify behavior.

In other words, IoC and DI are not necessary for loose coupling; you can have loose coupling with a Factory too

MyServiceImpl extends MyService {
    public dao1 
    public dao2;

    MyServiceImpl(){
       dao1 = DaoFactory.getDao1();
       ...
    }
    ....
}

here your service still only depends on DAO definitions and you use the factory to get implementations. The caveat is that your service is now coupled to the factory. You can make it more loose by passing a Factory into your constructor if you want....

Also, dont forget that Spring provides other useful functionalities, like its transaction management. That's incredibly helpful, even though you said for your app you don't need it.

国粹 2024-12-08 16:36:57

但无法理解 spring DI 如何松散耦合且侵入性较小
比工厂类?在这两种情况下我们都必须插入某种
获取我们核心程序中的目标代码。

Spring 使其侵入性较小,因为它使用反射来自动“注入/创建”依赖项。因此,您的代码不需要对工厂的引用。

Spring通常用于“类单例”对象创建。人们通常使用自定义工厂来创建临时丢弃对象(如请求对象)。
事实上,很多时候你会让 Spring 创建并注入你的自定义工厂(即工厂的工厂)。

But could not get how spring DI loosely coupled and less intrusive
than factory classes? in both the cases we have to insert some kind of
get object code in our core program .

Spring makes it less intrusive because it uses reflection to automatically "inject/create" the dependencies. Thus your code does not need a reference to a the factory.

Spring is generally used for "Singleton-like" object creation. People generally use custom factories for transient throw away object creation (like request objects).
In fact often times you will make Spring create and inject your custom factories (ie factory of a factory).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文