问题描述
我是Spring框架的新手。在我看到的大多数Spring教程中,Spring被描述为非侵入性。
I'm new to the spring framework. In most of the Spring tutorials I saw, Spring is described as "non-invasive".
入侵是什么意思?在Java中使用Spring有什么优点,是什么让它成为非侵入式?
What is meant by invasive? What are the merits of using Spring in Java and what makes it non-invasive?
推荐答案
如果IoC容器是侵入式的,那么它意味着您的代码需要明确了解依赖注入。例如,在Guice中,您使用 @Inject
注释(和其他)。这些注释比以前更加标准化,这很好 - 它意味着只需一组注释即可(至少在理论上)使您的代码可用于各种不同的侵入式IoC容器。
If an IoC container is invasive, it means your code needs to be explicitly aware of dependency injection. For example, in Guice you use the @Inject
annotation (and others). These annotations are more standardized than they used to be, which is good - it means with a single set of annotations you can (at least in theory) make your code available for use with various different invasive IoC containers.
使用非侵入式容器,您可以编写代码而不参考IoC ......所有内容都 通过对成员和注释的反思来确定即使您没有使用IoC也会出现。
With a non-invasive container, you can write your code with no reference to IoC at all... everything is just determined by reflection over members and annotations which would be present even if you weren't using IoC.
侵入式和非侵入式容器都有利弊 - 在代码中更具体,可以让您获得更多控制权例如,关于绑定的一些细节 - 但是值得注意区别。
There are pros and cons of both invasive and non-invasive containers - being more specific in code can give you more control over some of the details of binding, for example - but it's worth being aware of the difference.
这篇关于侵入的确切含义是什么?是什么让Spring非侵入性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!