21

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
JackVimal
  • 355
  • 1
  • 4
  • 13

9 Answers9

13

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • "If an IoC container is invasive, it means your code needs to be explicitly aware of dependency injection. " that means,The dependency information are showed or depicted in java code if IoC container is invasive....... is it right ? i'm can't understand what you're trying to say..... – JackVimal Mar 11 '13 at 07:45
  • 1
    @JackVimal: If you're using an invasive container (or using it in an invasive way) you'll be referring to annotations and possibly interfaces which are IoC specific, instead of writing code which is completely neutral, ambivalent to IoC. – Jon Skeet Mar 11 '13 at 08:09
  • 1
    You are describing IoC invasiveness, not Framework invasiveness. I think it's more accurate to say that an invasive IoC container requires you to use the framework's annotations or extend framework classes/interfaces, while a non-invasive container requires only that you use standard Java, i.e., javax.inject.Inject. We call Spring non-invasive because to use IoC you can just use standard Java (non-invasive). For Guice, you can use Guice's version of @Inject com.google.inject.Inject (invasive) or Guice's implementation of javax.inject.Inject (non-invasive). – Jason Mar 11 '13 at 14:24
5

check http://forum.springsource.org/showthread.php?27846-Spring-is-non-invasive

What does it really means?

"You are not forced to import or extend any Spring APIs, example Struts forces you to extend Action".

Of course in some areas, such as the web framework, it's impossible to avoid aplication code depending on the framework. But Spring consistently attempts to reach the non-invasive ideal where configuration management is concerned

ManMohan Vyas
  • 4,004
  • 4
  • 27
  • 40
5

Most of the java frameworks force you to extend one of their classes or implement one of their interfaces. Example of such an invasive programming model was EJB 2-era stateless session beans, earlier versions of Struts, WebWork, Tapestry.

Spring avoids littering your code with its API. Spring never forces you to implement a Spring-specific interface or extend a Spring-specific class. Instead, the classes often don’t have any indication that they’re being used by Spring.

public class HelloWorld {
    public String hello() {
      return "Hello World";
    }
}

This is a simple, Java class(POJO). Nothing indicates that it's a Spring component. Spring is non-invasive means this class could function equally well in a Spring application as it could in a non-Spring application. Although POJOs are simple they can be powerful.

One of the ways POJOs can become powerful in Spring is by using Dependency Injection.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Chinmay Patel
  • 430
  • 6
  • 12
4

Spring can be invasive and non-invasive, it's only up to you.

Non-invansive spring doesn't use annotations (for example @Autowired) nor its own classes (for example JdbcTemplate), it only uses configuration to wire your beans (simple POJO) together (you still have to initialize spring somehow in your code, which is a little bit invasive anyway). But you are free to drop spring without any significant code changes. Invasive spring, on the other hand, provides you template classes (for persistence, web services,...), annotations and other stuff you cannot simply leave behind without refactoring (if you use them).

EDIT: Some say spring is not invasive, because it doesn't force you to implement interfaces nor extend classes. For me a framework is not invasive, if it can be easily replaced.

Kojotak
  • 2,020
  • 2
  • 16
  • 29
  • More correctly, "Non-invasive Spring doesn't use Spring annotations". For example, by replacing `@Autowired` with `@Inject` you get the same functionality without using a Spring annotation. See http://stackoverflow.com/questions/7142622/what-is-the-difference-between-inject-and-autowired-in-spring-framework-which – Paul Oct 25 '13 at 18:30
1

An invasive framework means that you are force to extend one of their classes or implement one of their interfaces. You can see that in frameworks like Struts or EJB2. Spring is not-invasive because avoids that. At worst, a class may be annotated with one of Spring's annotation, but it´s otherwise a POJO. So only you need a POJO like that:

package com.habuma.spring;
 public class HelloWorldBean {
  public String sayHello() {
return "Hello World";
 }
}
Diego Virgüez
  • 154
  • 2
  • 12
0

Spring framework is said to be a non-invasive means it doesn’t force a programmer to extend or implement their class from any predefined class or interface given by Spring API, in struts we used to extend Action Class right that’s why struts is said to be invasive.

In case of struts framework, it will forces the programmer that, the programmer class must extend from the base class provided by struts API.

Eugene Loy
  • 12,224
  • 8
  • 53
  • 79
0

Use @Resource and @Inject instead of @Component and @Autowired. It is really useful and is standardized across DI frameworks like Spring and Guice.

0

Invasive meaning - spreading very quickly all around. Spring author says frameworks like EJB, Struts etc are full of framework specific code which is hard to migrate to other frameworks which is against Java philosophy of write once and run anywhere. Spring says it noninvasive because it uses POJO to write server side so it can be migrated to any platform. But as suggested by at least 2 contributor it in this post, Spring is cancer contrary to what it claims

spring is a cancer to a project that once it has infected your project you can't do anything without hacking at spring config files ... non-invasive is pretty misleading on that front! – user177800 Mar 11 '13 at 7:18

Shailesh
  • 405
  • 1
  • 5
  • 18
0

Claiming that Spring is non-invasive is like claiming that the earth is flat. You completely deny reality based on nothing else than your personal bias.

Spring is not only invasive to your code base, it is also invasive to your brain. It will annoy the hell out of you when you have built a decent confidence in searching the web for information and solutions for your programming problems. I have done serious programming work in some 10 languages and shells, and almost always managed to find useful information, answers and hints to solve my problems. Usually I manage to find convergence towards a decent solution pretty fast, and sometimes after a bit of digging. Not so with Spring. A once popular scripting language touted the slogan "there's more than way to do it". For Spring, you will mostly find many ways how not to do it, or many ways that will not work in your particular setup.

I do not doubt that Spring has real value. I do see that. But it comes with way too much magic, that works well as long as it works, but once it fails, the tooling and documentation often fail as well.

zberd
  • 21
  • 1
  • 4