19

When people mention that Spring is a lightweight containter compared to other frameworks, do they mean? That it occupies less memory in the system or it does not have the operations like start stop that we have for EJB containers and it doesn't use a special container?

What makes Spring a lightweight container?

skaffman
  • 398,947
  • 96
  • 818
  • 769
ronan
  • 4,492
  • 13
  • 47
  • 67

5 Answers5

10

Whether it is "lightweight" or "heavyweight", it is all about comparison. We consider Spring to be lightweight when we are comparing to normal J2EE container. It is lightweight in the sense of extra memory footprint for the facilities provided (e.g. Transaction Control, Life Cycle, Component dependency management)

However, there are sometimes other criteria to compare for the "weight" of a container, e.g. intrusiveness in design and implementation; facilities provided etc.

Ironically, Spring is sometimes treated as heavy weight container when compared to other POJO-based container, like Guice and Plexus.

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • 4
    Saying Spring is lightweight *compared to* EJB 1 & 2 is a correct statement... but the entire double bookshelf version of Encylopedia Britanica is lightweight compared to EJB 1 & 2 so no prize for being lighter than EJB 1 & 2. Having recently been forced to use Spring has proven all my expectations true so far - Spring is definitely a heavyweight, highly intrusive, hard to debug (can your debugger step through XML or annotations? Mine neither) and in many areas where it intrudes, redundant, framework compared to other solutions available. Layers upon layers of annotated "OJOs" (-P: not Plain) – Volksman Mar 05 '15 at 13:24
  • If you work with enterprise Java development from early 2000s, you will know it is not "meaningless" to compare to J2EE (not only EJB1&2 to be explicit). At that time, the only "proven" enterprise application framework is J2EE. Spring is the first container alternative that is proven to be workable. If you look at J2EE and other alternative container (e.g. ever heard of Avalon?), Spring is really light-weight and non-instrusive. Of course there are other even-lighter-weight containers comes out afterwards but the DI of Spring gives the foundation of all such kind of light-weight container. – Adrian Shum Mar 06 '15 at 01:54
  • 2
    btw, whether you want to make Spring heavy weight, intrusive or hard to debug, it is just a matter of choice. In most case, you can includes parts that you need (e.g. if you just need DI, you do not need to include Spring MVC or even Spring AOP). Intrusiveness in a lot of case also related to your choice: you may use Spring-specific interfaces or helpers, but most of them can be achieved in alternative non-intrusive manner. For debug, Spring does provide Java-base configuration which I guess can solve your concern. – Adrian Shum Mar 06 '15 at 01:58
9

Spring calls itself 'lightweight' because you don't need all of Spring to use part of it. For example, you can use Spring JDBC without Spring MVC.

Spring provides various modules for different purposes; you can just inject dependencies according to your required module. That is, you don't need to download or inject all dependencies or all JARs to use a particular module.

enter image description here

If you want to run a Java EE application, you can't just create a small application that will run on its own. You will need a Java EE application server to run your application, such as Glassfish, WebLogic or WebSphere. Most application servers are big and complex pieces of software, that are not trivial to install or configure.

You don't need such a thing with Spring. You can use Spring dependency injection, for example, in any small, standalone program.

Michael
  • 41,989
  • 11
  • 82
  • 128
Girdhar Singh Rathore
  • 5,030
  • 7
  • 49
  • 67
7

I think "lightweight" is mostly a buzz-word. It's meaning is highly subjective and based on context. It can mean "low memory footprint", it can be low execution overhead, low start-up overhead. People also use it to differentiate between some perceived level of complexity and/or learning-curve. In any case, it's assuredly relative as there is no defined point on any scale where "light" becomes "heavy" in terms of "weight".

I personally think it's a dangerous word since it has no real, quantifiable meaning. It's something people throw into architecture proposals to beef up the "pro" section of a certain framework they want to use anyway. If you see or hear it being used in any such situation, it's a perfect opportunity to ask "what does that mean?". If you get an angry or frustrated response (combined with rolling of eyes and shaking of head), it means that the person has decided on a certain architecture, but hasn't managed to formulate coherent or objective reasons for it.

EDIT: not sure I would categorize spring as a "container" either, but that's a similar apples and oranges discussion. I'd call it a framework.

pap
  • 27,064
  • 6
  • 41
  • 46
  • I agree, Spring is a framework, not a container. Containers in the J2EE/Servlet world represent 'application' containers - or the thing within which web applications run. Spring is a framework that some Java developers choose to add to their Java applications. Others achieve the same thing in different ways with different frameworks kind of like Spring, some use extremely lightweight frameworks and others still use no frameworks (rare) – Volksman Mar 08 '15 at 07:58
2

Spring is light weight becouse other J2ee container especially EJB2.1 require more configuration, It can have lot of do nothing code to ,it have complex directory structure for packing applications, overall it took extra memory;on other hand spring minimizes all this things.so it light weight.

0

I think one can also say that spring is light weight because it uses POJO(Plain old java object) .POJO class does not require to implement,extends technologies specific API(Interfaces,Classes) or it is not bounded to any technology specific API

Rajat Agrawal
  • 141
  • 1
  • 4
  • 13