Why Spring framework???
Spring is a lightweight open source framework written by Rod Johnson in 2000 while he was working as an independent consultant for customers in the financial industry in London. It has been widely accepted in industry.
There are many java frameworks available in market; widely accepted ones are struts as MVC framework, Hibernate as ORM framework, EJB and frameworks like velocity and tiles at a view layer. Each of these frameworks serves specific purpose and also requires a container. And integrating them in one application is a pain for programmer. Spring unlike those frameworks is a container as well as a framework and supports integration with all other frameworks by means of some configuration. It is framework because it has its own implementation of MVC and container because it can manage your implementation of ORM and MVC as well as it also integrates well with EJBs and in that case you don't require EJB container. Spring becomes container of EJBs there.
On top of this spring gives some unique features like IoC and AOP.
Inversion of Control:
Spring is an IoC container," Jeff explained. "That stands for Inversion of Control, which enables you to inject dependencies by declaring them in an XML file."
Just to give you an example, I have an application which accepts payment from end users, I have to calculate tax while calculating payment. Here Payment and TaxCalculator are two beans. Every time i instantiate Payment, i need TaxCalculator which is dependency. One option is to whenever i create Payment, i can create TaxCalculator and can give a reference of it to Payment. Instead if you use spring, you just need to define in configuration that Payment has a dependency on TaxCalculator. So whenever Payment is instantiated, it will get TaxCalculator instance from session. I won't have to code anything for that.
Note: This can only be used for dependency of service classes.
Another example of IoC is, You have a TaxCalculator which calculates tax, but its implementation will change country by country. So while deploying application, you specify which country you are deploying it for, And it will automatically pick up TaxCalculator of that country by simple means of configuration, which reduces programmers overhead of writing factory implementation or any other implementation to have this capability.
Spring wraps all checked exceptions into run time exceptions. So you don't have to unnecessarily catch them when you are sure they won't occur. In fact new versions of EJB, struts and Hibernate also does this.
Aspect Oriented Programming
Easy integration of AOP is unique feature that spring cones with. AOP enables you to do lots of interesting things.
- You can add member variables or member methods to any class without actually modifying definition of that class.
- You can introduce Aspects into your application. Aspect is a behavior that applies to many of your classes and implementation of that behavior is same for all those classes. You just have to create Aspect and introduce it to set of classes to which that behavior applies. Ex: Logging behavior
- You can handle your exceptions in a better way. You can create Aspects for all exceptions and handle them at one place. Then by configuration apply those Aspects to all classes. Doing this you don't have to handle exceptions separately in all classes which saves lots of programming effort.
Spring has a nice way of managing access control. Once you have Authentication and Authorization modules in place, AccessDecisionManager provided by spring lets you define access control per bean through configuration. So if you want to change access control for any information in future, you just have to change configuration and you are done.
Scheduled job or crone job
Spring has a mechanism by which you can create crone job or scheduled job. It uses Java Time APIs. It also supports quartz to create scheduled job.
When you use hibernate and struts, and do not use any features of spring as such, Spring becomes a container for you which integrates both of them via configuration. Spring makes everything as just configuration in which case your configuration file can become too large to manage. To address this, Spring allows you to have array of configuration files, list them all in one file and supply that file as a configuration file. Spring will do rest for you.
Spring also lets you expose any service you have written as a web service or Remote Method by means of configuration.
Spring divides whole web application into different tier. And handles each tier separately. So that you can integrate any component at any layer and can also replace a component at any layer without affecting other layers or other components. That is the only reason of ease of integration of struts or hibernate or any other framework with spring.
So if you know struts, hibernate, EJB, RMI technologies and want to use one of them or all of them or some of them in your application then integration of those technologies is just a matter of configuration using spring. otherwise you will run into writing whole lot of code which is pain and difficult too. Also there is huge community built around spring that helps a lot in figuring out or solving problems that you may face while development.
For detailed information on spring framework visit springframework.org.