为什么依赖注入需要另一种语言?

发布于 2024-12-12 03:13:36 字数 201 浏览 0 评论 0原文

我很擅长用 Java 编程,但对 Spring 还很陌生。我一直在阅读有关依赖注入/控制反转的内容(并在过去几个月中将其与 Spring 一起使用),但我无法弄清楚是否需要单独的语言(xml/spring)来完成它。

在 Java 中创建一个名为 DependencyHandler 的单例并保持所有内容都使用同一语言有什么问题吗?使用 xml/Spring 有哪些优势?

I'm comfortable programming in Java, but am fairly new to Spring. I've been reading about dependency-injection/inversion of control (and using it with Spring for the past few months), but I can't figure out the need for a separate language (xml/spring) to accomplish it.

What is wrong with creating a singleton in Java called DependencyHandler, and keeping everything in the same language? What are the advantages I get by using xml/Spring?

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

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

发布评论

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

评论(5

最后的乘客 2024-12-19 03:13:36

依赖注入不需要单独的语言。

Spring 是一个 Java 框架,历史上需要在 xml 中进行配置。现在您可以使用 xml 或 java 注释来配置它。

Google 的 Guice 是一个简单的依赖注入框架,所有配置都在 Java 中。

Dependency Injection does not require a separate language.

Spring is a framework for Java that historically required configuration in xml. Now you can configure it using xml, or java annotations.

Google's Guice is a simple dependency injection framework that has all configuration in Java.

梦途 2024-12-19 03:13:36

对于特定用途,自定义语言(以 xml 形式)可能比 Java 更好,这是有正当理由的。但对于 DI 来说,原因很牵强,事实上,这并不是真正的原因。

从无数快乐的 Spring 用户的见证来看,压倒性的原因是他们认为 xml 不是代码。他们厌倦了编写样板 Java 代码,因此很高兴切换到样板 xml。这让他们很高兴。

在经济问题上,人类并不理性。我们拥有复杂的系统,可以循环转移资源,在这种毫无意义的浪费中找到安慰和安全感。

但我认为幸福是最重要的,无论它有多迟钝。

There can be legitimate reasons why a custom language (in xml) can be better than Java for a specific purpose. For DI though, the reasons are stretchy, and in fact, not the real reasons.

From countless testimonies from happy Spring users, the overwhelming reason is that they somehow think xml is not code. They are so tired of writing boilerplate Java code, they are happy to switch to boilerplate xml. And that makes them happy.

Human beings are not rational when it comes to economic matters. We have elaborate systems that transfer resources in circles, finding comfort and security in such pointless waste of efforts.

But I guess happiness is the most important thing, however retarded it could be.

懒的傷心 2024-12-19 03:13:36

您也可以创建使用 Java 语法的依赖注入框架。例如,看看 Google Guice 即可。

You can make dependency injection frameworks that use Java syntax, too. Just look at Google Guice, for example.

七颜 2024-12-19 03:13:36

我将专门回答 XML 的“好处”部分,尽管数量不多。

将配置与代码完全分离可以从源代码中删除所有框架工件,这可能是有益的。

创建影响配置文件的工具链更容易(不是荒谬的,但足以值得注意):属性加载/替换、配置感知 GUI 配置编辑器、文档生成等

。配置不是散布在代码库中,而是在一组文件(或单个文件)中。这不是纯 XML 的 vitrue,它取决于事物的配置方式。

我认为某些类型的配置比其他类型更适合外部配置。我根据给定的要求、框架允许的内容以及框架如何处理配置方面的内容进行选择。

I'll answer the "benefits" part for XML specifically, although there aren't many.

Having configuration completely separate from code removes all framework artifacts from the source, which can be beneficial.

It's easier (not ridiculously so, but enough to be noteworthy) to create toolchains that affect configuration files: property loading/replacement, config-aware GUI config editors, documentation generation, etc.

Centralized configuration; instead of config being strewn around the codebase, it's in a group of files (or single file). This isn't an XML-only vitrue, it depends on how things are configured.

I think some types of configuration lend themselves to external configuration more than others. I choose based on what seems appropriate given the reqs, what the framework allows, and how the framework handles config aspects.

失退 2024-12-19 03:13:36

Spring 只是在大型项目中管理依赖注入的一种简单方法。

但是您可以通过在类上使用静态工厂方法来注入依赖项:

public class Foo
{
    public Foo static mkFoo(/* dependencies */)
    {
        // assign dependencies to members
    }

    // ordinary class stuff
}

然后,每当您需要 Foo 时,只需执行 Foo.mkFoo(/*dependency*/) 即可。无需弹簧

在 Java 中创建一个名为 DependencyHandler 的单例并保持所有内容都使用同一语言有什么问题吗?

在单个类中处理所有依赖项很快就会变得混乱,并且会导致与所有其他类的耦合。但这并不是不在普通 java 中处理 DI 的理由。

Spring is just an easy way to manage dependency injection in large projects.

But you can inject dependencies by using a static factory method on your class:

public class Foo
{
    public Foo static mkFoo(/* dependencies */)
    {
        // assign dependencies to members
    }

    // ordinary class stuff
}

Then you just do Foo.mkFoo(/*dependencies*/) whenever you want a Foo. No spring required.

What is wrong with creating a singleton in Java called DependencyHandler, and keeping everything in the same language?

Handling all your dependencies in a single class is going to get messy quickly, and will result in coupling with all of your other classes. But that isn't a reason to not handle DI in plain java.

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