What is the exact meaning of invasive? and what makes Spring non-invasive?

JackVimal picture JackVimal · Mar 11, 2013 · Viewed 9.4k times · Source

I'm new to the spring framework. In most of the Spring tutorials I saw, Spring is described as "non-invasive".

What is meant by invasive? What are the merits of using Spring in Java and what makes it non-invasive?

Answer

Jon Skeet picture Jon Skeet · Mar 11, 2013

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.

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.