Spring framework is not new and is used widely, this article addresses very basic of core component of Spring which works on Dependency Injection (DI) concept. Spring was invented by Rod Johnson. It was released with its book “Expert One-on-One J2EE Design and Development” in October 2002. Now Spring Framework is also available for MS .Net Platform.
Spring Framework is truly modular framework and it contains multiple components i.e. Spring-core, Spring AOP, Spring ORM etc. if you want to use only Spring MVC component, you can remove all other packages and use only required packages (spring-core and spring-MVC). All components of spring are built on/around Spring-core (Although some components/classes can be used without Spring-core i.e. Spring JDBC Component) .
Major Components of Spring Framework

The above figure show major components of Spring (There are many more components under Spring umbrella, but I have specified major components as per my understanding). As I said you can use any components as and when required. Rather than explaining about all components briefly, I’ll explain Spring-core only.
Please note that any of these components (except Spring MVC) do not provide something which already exists. For example Spring ORM is not another ORM library but it allows you to plug any ORM like JBoss Hibernate, Oracle TopLink, JPA etc. So what does Spring ORM do??? Spring ORM with Spring-core capability provides easy, flexible and convenient way to use desired ORM in Spring enabled application, it also reduces boiler-plate code.
So you can consider Spring as an integration framework.
In my opinion, working or practical example always helps in understanding new technology better. So for this article, consider we have “BookProvider” class which provides details of “Book” by its ISBN number, “BookService” interface can be considered as DAO (Data Access Object) interface, and its implementations “AmazonBookService” and “LocalBookService” which can fetch book from Amazon web service or Local book database.
Have a look at the source structure.

Spring can be configured by either XML or annotations. But in this example we will stick to xml based configuration (which is actually preferred and promoted by Spring community). In this structure config/spring.xml is Spring configuration file.
Before moving ahead, we should understand what is DI.
Dependency Injection (DI):
Dependency injection (DI) is also known as IoC (Inversion of Control). DI is very basic concept, yet very powerful. BookProvider requires BookService. Can you think how BookProvider will get instance of BookService?
One way is BookProvider will ask some service locator (static or singleton) class to locate BookService instance typically ServiceLocator.getBookService();, But the problem is now BookProvider is coupled with ServiceLocator’s getBookService() method. So while testing (unit testing) BookProvider you have to provide implementation of BookService ServiceLocator. Not that only, now your BookProvider is not reusable without reusing ServiceLocator.
Another option is having method “void setBookService(BookService service)” in BookProvider class or passing BookService instance in constructor. And parent class which creates BookProvider instance will provider BookService instance also. Understanding problem here may be bit tricky or hard, so concentrate on each word. So in this case, BookProvider dependency of BookService is moved to parent class (For example Library) as parent class is now dependent on BookService, reusing or testing parent class becomes hard or infeasible.
So if we use DI pattern, these kinds of unwanted dependencies can be injected by DI tool or container when object (which requires services/instances of other object, i.e. BookProvider) is created.
I hope you understand fundamentals of DI. If you are not clear you can try googling Dependency Injection Introduction or Tutorial. (Lazy guys like me, click here )
Example with explanation:
I highly recommended trying it out while reading or after reading. I have provided most of the code here OR you can download code (with Netbeans IDE project files); I omitted trivial java code only. If you are using Netbeans as IDE, Spring-core is already available as in-built library, so no need to download it, otherwise you have to download it from Spring Source Community Website .
Spring’s configuration file: spring.xml ( Sorry, Its image instead of text, I will take care from next time, you know I am new to blogging and authoring )

<beans> </beans> is parent/container for all <bean> </bean> definition.
Spring container (Spring-core manages object instances) loads each <bean> as a singleton (this behavior is configurable, but out of this article’s scope). In <bean>, “id” must be unique. And object of class specified in “class” property will be instantiate and maintained by spring. If class is not having zero-arg constructor, then required reference or value must be specified in <constructor-arg> .
i.e.
[sourcecode language="xml"]
[/sourcecode]
In above example, value (Baleno) is of type String. If constructor requires other object as argument then instead of “value”, “ref” should be used, “ref” is used for referring to other beans. You can see usage of “ref” in example spring.xml‘s “bookProvider” bean. So Spring allows us to use classes without changing any of its code, very helpful when you want to use external libraries or legacy code/classes.
In spring.xml, we have created two beans one is “bookService” which is instance of AmazonBookService class. Another bean “bookProvider” is instance of BookProvider class, and we injected “bookService bean” into “bookService property of BookProvider”. Understand the difference here, “bookService property of BookProvider” means that there is a setBookService(BookService service) method in BookProvider, where “bookservice bean” which is instance of AmazonBookService will be injected.
BookProvider.java
[sourcecode language="java"]
public class BookProvider {
private BookService bookService;
//BookService will be set by Spring using this method.
public void setBookService(BookService bookService) {
this.bookService = bookService;
}
public BookProvider() {
}
public Book getBook(String isbnNo) {
return bookService.getBook(isbnNo);
}
}
[/sourcecode]
BookService.java
[sourcecode language="java"]
public interface BookService {
public Book getBook(String isbnNo) throws IllegalArgumentException;
}
}
[/sourcecode]
AmazonBookService.java
[sourcecode language="java"]
public class AmazonBookService implements BookService{
public Book getBook(String isbnNo) {
//Web service call goes here, or use this dummy implementation.
if(!”1-933988-13-4″.equals(isbnNo)) {
throw new IllegalArgumentException(“Dummy impl: Book not found with ISBN: ” + isbnNo +”, use 1-933988-13-4 only.”);
}
System.out.println(“Fetching Book [ISBN: " + isbnNo + "] from Amazon”);
return new Book(“Spring In Action – Second Edition”, “1-933988-13-4″, “CRAIG WALLS”);
}
}
[/sourcecode]
main() method

For brevity, I have not shown Book and LocalBookService implementation. BookProvider and LocalBookService are very simple POJO (Plain Old Java Object), so I am omitting its explaination. In main() method, first statement creates instance of ClassPathXmlApplicationContext instance, which will load spring.xml. ClassPathXmlApplicationContext is nothing but specialized sub class of BeanFactory. Second statement retrieves “bookProvider” bean from BeanFactory. Now you can call (as shown in main() method) getBook(isbnNo) method, because BookService (actually ) instance is automatically injected to BookProvider by Spring.
Output of execution is
—————————–
Fetching Book [ISBN: 1-933988-13-4] from Amazon
Book found: Spring In Action – Second Edition, ISBN: 1-933988-13-4, Author: CRAIG WALLS
—————————–
This is the core functionality of Spring framework, on top of this, other features and components are implemented.
Spring DI is powerful technique, let me just give you one example, say now we want to change BookService, instead of Amazon web service we want to use our own book information database for book details. So you just need to change value of “bookService” bean’s class attribute to LocalBookService from AmazonBookService in spring.xml. Thats all. If you run the program, output will show that data is fetched from Local database.
Output of execution is
—————————–
Fetching Book [ISBN: 1-933988-13-4] from Local Database
Book found: Spring In Action – Second Edition, ISBN: 1-933988-13-4, Author: CRAIG WALLS
—————————–
Conclusion:
If you are not clear with overall usage and benefit of Spring, it is okay. Because this article is an attempt to get started with Spring. In Spring intensive production application, all beans are wired with each other, so in application you will not need to get any beans manually.
Spring
- Decouples classes which allow their testing independently and reusing them.
- Encourages “Programming to interface” principle.
- Allows POJO programming, by not adding any spring specific code in spring beans.
- Allows legacy classes to be used.
If you found Spring Framework interesting and useful, please try it out.
You can download spring framework from www.springsource.org, Also there is plenty of documentation available to get started.
Source code of this article : Download as Zip
Please feel free to comment for any query, suggestion, compliments or complaints.
Stay tuned for next post. 
Cheers,