tags:

views:

1306

answers:

9

I hear a lot about spring, people are saying all over the web that Spring is good framework for web development. But what exactly is it for? How can I use it for my Web-Java application any examples.

A: 

Well, this one is a bit technical...

http://en.wikipedia.org/wiki/Spring_Framework

But this one is a bit more friendly...

http://en.wikipedia.org/wiki/Spring_Framework

the.jxc
Um... unless I'm missing something, I don't think those are supposed to be the same link.
Isaac Waller
Yeah, before asking this question I tried to read Wikipedia's article, but they were kind of technical that's why i gave up and asked you guys.
Maksim
+2  A: 

Spring is great for gluing instances of classes together. You know that your Hibernate classes are always going to need a datasource, Spring wires them together (and has an implementation of the datasource too).

Your data access objects will always need Hibernate access, Spring wires the Hibernate classes into your DAOs for you.

Additionally, Spring basically gives you solid configurations of a bunch of libraries, and in that, gives you guidance in what libs you should use.

Spring is really a great too. (I wasn't talking about Spring MVC, just the base framework).

stevedbrown
+3  A: 

Spring contains (as skaffman rightly pointed out) a MVC framework. To explain in short here are my inputs. Spring supports seggregation 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, I-batis 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

Priyank
Excellent explanation. Thanks for your answer and time!
Maksim
Spring *contains* an MVC framework. But it's a lot, lot more than that.
skaffman
Without wishing to nitpick too much, WebMVC is part of the core spring distro. Webflow, RCP et al are not.
skaffman
+1  A: 

Spring started off as a fairly simple dependency injection system. Now it is huge and has everything in it (except for the proverbial kitchen sink).

But fear not, it is quite modular so you can use just the pieces you want.

To see where it all began try:

http://www.amazon.com/Expert-One-Design-Development-Programmer/dp/0764543857/ref=sr_1_1?ie=UTF8&s=books&qid=1246374863&sr=1-1

It might be old but it is an excellent book.

For another good book this time exclusively devoted to Spring see:

http://www.amazon.com/Professional-Java-Development-Spring-Framework/dp/0764574833/ref=sr_1_2?ie=UTF8&s=books&qid=1246374863&sr=1-2

It also references older versions of Spring but is definitely worth looking at.

Pablojim
+39  A: 

Basically Spring is a framework for Dependency Injection which is a pattern that allows to build very decoupled systems. I'll try to explain you the simplest I can (this isn't a short answer).

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 interface 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 doesn't have 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 last line by:

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 a XML file, 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 an XML like this:

<bean id="userLister" class="UserListerDB" />

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

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 to Spring Documentation, in my opinion is the best Spring book ever.

victor hugo
+1 for great example and explication.. thanks !
Agusti-N
+3  A: 

What you'd probably want in a web application with Spring -

  • Spring MVC, which with 2.5+ allows you to use POJOs as Controller classes, meaning you don't have to extend from any particular framework (as in Struts or Spring pre-2.5). Controller classes are also dead simple to test thanks in part to dependency injection
  • Spring integration with Hibernate, which does a good job of simplifying work with that ORM solution (for most cases)
  • Using Spring for a web app enables you to use your Domain Objects at all levels of the application - the same classes that are mapped using Hibernate are the classes you use as "form beans." By nature, this will lead to a more robust domain model, in part because it's going to cut down on the number of classes.
  • Spring form tags make it easier to create forms without much hassle.

In addition, Spring is HUGE - so there are a lot of other things you might be interested in using in a web app such as Spring AOP or Spring Security. But the four things listed above describe the common components of Spring that are used in a web app.

bpapa
A: 

Spring is for procreation.

KevMo
+1  A: 

Spring is a good alternative to Enterprise JavaBeans (EJB) technology. It also has web framework and web services framework component.

fastcodejava
+4  A: 

Very short summarized, I will say that Spring is the "glue" in your application. It's used to integrate different frameworks and your own code.

Johan