What exactly is Spring Framework for? [closed] What exactly is Spring Framework for? [closed] java java

What exactly is Spring Framework for? [closed]


Basically Spring is a framework for which is a pattern that allows building very decoupled systems.

The problem

For example, suppose you need to list the users of the system and thus declare an interface called UserLister:

public interface UserLister {    List<User> getUsers();}

And maybe an implementation accessing a database to get all the users:

public class UserListerDB implements UserLister {    public List<User> getUsers() {        // DB access code here    }}

In your view you'll need to access an instance (just an example, remember):

public class SomeView {    private UserLister userLister;    public void render() {        List<User> users = userLister.getUsers();        view.render(users);    }}

Note that the code above hasn't initialized the variable userLister. What should we do? If I explicitly instantiate the object like this:

UserLister userLister = new UserListerDB();

...I'd couple the view with my implementation of the class that access the DB. What if I want to switch from the DB implementation to another that gets the user list from a comma-separated file (remember, it's an example)? In that case, I would go to my code again and change the above line to:

UserLister userLister = new UserListerCommaSeparatedFile();

This has no problem with a small program like this but... What happens in a program that has hundreds of views and a similar number of business classes? The maintenance becomes a nightmare!

Spring (Dependency Injection) approach

What Spring does is to wire the classes up by using an XML file or annotations, this way all the objects are instantiated and initialized by Spring and injected in the right places (Servlets, Web Frameworks, Business classes, DAOs, etc, etc, etc...).

Going back to the example in Spring we just need to have a setter for the userLister field and have either an XML file like this:

<bean id="userLister" class="UserListerDB" /><bean class="SomeView">    <property name="userLister" ref="userLister" /></bean>

or more simply annotate the filed in our view class with @Inject:

@Injectprivate UserLister userLister;

This way when the view is created it magically will have a UserLister ready to work.

List<User> users = userLister.getUsers();  // This will actually work                                           // without adding any line of code

It is great! Isn't it?

  • What if you want to use another implementation of your UserLister interface? Just change the XML.
  • What if don't have a UserLister implementation ready? Program a temporal mock implementation of UserLister and ease the development of the view.
  • What if I don't want to use Spring anymore? Just don't use it! Your application isn't coupled to it. Inversion of Control states: "The application controls the framework, not the framework controls the application".

There are some other options for Dependency Injection around there, what in my opinion has made Spring so famous besides its simplicity, elegance and stability is that the guys of SpringSource have programmed many many POJOs that help to integrate Spring with many other common frameworks without being intrusive in your application. Also, Spring has several good subprojects like Spring MVC, Spring WebFlow, Spring Security and again a loooong list of etceteras.

Hope this helps. Anyway, I encourage you to read Martin Fowler's article about Dependency Injection and Inversion of Control because he does it better than me. After understanding the basics take a look at Spring Documentation, in my opinion, it is used to be the best Spring book ever.


Spring contains (as Skaffman rightly pointed out) a MVC framework. To explain in short here are my inputs.Spring supports segregation of service layer, web layer and business layer, but what it really does best is "injection" of objects. So to explain that with an example consider the example below:

public interface FourWheel{   public void drive();}public class Sedan implements FourWheel{   public void drive()   {      //drive gracefully   }}public class SUV implements FourWheel{   public void drive()   {      //Rule the rough terrain   }}

Now in your code you have a class called RoadTrip as follows

public class RoadTrip{    private FourWheel myCarForTrip;}

Now whenever you want a instance of Trip; sometimes you may want a SUV to initialize FourWheel or sometimes you may want Sedan. It really depends what you want based on specific situation.

To solve this problem you'd want to have a Factory Pattern as creational pattern. Where a factory returns the right instance. So eventually you'll end up with lots of glue code just to instantiate objects correctly. Spring does the job of glue code best without that glue code. You declare mappings in XML and it initialized the objects automatically. It also does lot using singleton architecture for instances and that helps in optimized memory usage.

This is also called Inversion Of Control. Other frameworks to do this are Google guice, Pico container etc.

Apart from this, Spring has validation framework, extensive support for DAO layer in collaboration with JDBC, iBatis and Hibernate (and many more). Provides excellent Transactional control over database transactions.

There is lot more to Spring that can be read up in good books like "Pro Spring".

Following URLs may be of help too.
http://static.springframework.org/docs/Spring-MVC-step-by-step/
http://en.wikipedia.org/wiki/Spring_Framework
http://www.theserverside.com/tt/articles/article.tss?l=SpringFramework


Old days, Spring was a dependency injection frame work only like (Guice, PicoContainer,...), but nowadays it is a total solution for building your Enterprise Application.

The spring dependency injection, which is, of course, the heart of spring is still there (and you can review other good answers here), but there are more from spring...

Spring now has lots of projects, each with some sub-projects (http://spring.io/projects). When someone speaks about spring, you must find out what spring project he is talking about, is it only spring core, which is known as spring framework, or it is another spring projects.

Some spring projects which is worth too mention are:

If you need some more specify feature for your application, you may find it there too:

  • Spring Batch batch framework designed to enable the development of
    batch application
  • Spring HATEOAS easy creation of REST API based on HATEOAS principal
  • Spring Mobile and Spring Andriod for mobile application development
  • Spring Shell builds a full-featured shell ( aka command line) application
  • Spring Cloud and Spring Cloud Data Flow for cloud applications

There are also some tiny projects there for example spring-social-facebook (http://projects.spring.io/spring-social-facebook/)

You can use spring for web development as it has the Spring MVC module which is part of Spring Framework project. Or you can use spring with another web framework, like struts2.