A Simple Spring Tutorial - I

We are having very curiosity about spring that what is it ?
how to use it ?
how it will reduce my code :-) (as being a developer I always think) and because of these questions may be you are reading my blog.

Thank you friends for coming here.

Well Spring is a magic box which is full of :
Container, Web Services, MVC, Webflow etc etc
in short Spring is all a developer needs :-)


Usually being a developer I hate to attend boring lectures and theories but would like to learn the core concepts of course. So rather then diving very deep in all boring theories and big words let's start our learning process.

Core Container
The core container provides fundamental functionality of Spring. It's primary component is the 'BeanFactory' an implementation of the factory pattern. The BeanFactory applies the IoC pattern to separate an application’s configuration & dependency specification from the actual application code. -confused :-( (hey I am not talking about you buddy.)

Rather then getting jumbled myself with this complex definition would prefer to code with my previous style and here is the requirement.

"Develop cake interface which give me a cake, when I ask for it." - You know who can order me like this.

So my classes are :
  • Interface Cake
  • Class BlackForest
  • Class CakeMain
Cake.java
BlackForest.java
CakeMain.java

let's see what is happing when we run the main class :

Our application creates the object(s) of BlackForest in heap and assign it with reference of cake, am i right ?
  • Every time method called, a new object of BlackForest is being created as we don’t implemented the singleton design pattern otherwise the same object would be used.
  • Once a class is complied, and if we want to change the implementation i.e. now I want Pineapple flavoured cake and not the previous BlackForest, we have to rewrite & compile the class for changing the dependency I mean the implementation.
  • It is also an overhead on our application to create the objects for itself by itself.

Container : Hey why are you looking so sad ?
Application : I was born to handle complex business logic, automating systems, calculations etc but for all these I need so many objects of different types and you know I have to prepare these object also :-( there is no one there to help me out.
Container : Oooo So sad !

This was the scenario one.

Now lets try to think differently...
"Oooo one more concept to learn I am happy with my previous one." this is not your feeling right b'coz you are here to learn new things.
  • When you need an object of BlackForest, some one is there with plate, again he is ready to serve you your desired flavour whether BlackForest,Pineapple or anything you want. "Fantastic..."
  • Again you get flexibility to define your dependency outside your classes. I mean just define your reference inside your class and container will deicide according to your instructions to him which object to assign.
  • In addition you get the flexibility to say I want to implement Singleton for this class, I don’t want to implement for that class. I want to create object of this class at class loading and not of that object ;-)
Now lets see how spring core container helps us.... hey wait if you didn't download the spring framework then download it first.
please add the spring.jar in your build path.
now we only need to add is one xml file, this file contains the configuration for beans.

application-contex.xml

Please notice in this xml file in side the beans tag there is a bean tag.
In this bean tag we specified following two properties :
  • id : id is the actual object reference name specified in our class(where we want the object of specified class) in our case this is "cake" which we defined in our main class.
  • class :the full qualified class name in our case that is tutorial1.BlackForest. Please notice that there is no need to give the .java extention.
Please make the following changes in CakeMain

CakeMain.java
We need to note few things about the new Main class

  • We created only the reference of Cake interface.
  • We created an object of ApplicationContext with the help of ClassPathXmlApplicationContext class and passed the name of our xml file to it.
  • Here AppliationContext and ClassPathXmlApplicationContext both are provided by spring framework.
  • Then we call method getBean(String beanId) of ApplicationContext class, return type of this method is Object so we need to cast it to Cake as we know that by passing "cake" it will return a Cake implementation.(Our dependency is getting injected with the help of spring according to
  • configuration provided in xml path)
  • The last print statement will tell us the name of Cake we get.

Can you tell me please now instead of BlackForest I want the new Pineapple, what the changes we need to do ? exactly.... first of all

we need to add new implementation for Peneapple.
Pineapple.java

and we need to change the class name from BlackForest to Pineapple in our application-context.xml, the new xml is
application-context.xml
That means here we are changing implementation i.e dependency but we do not need to change the Main class what we need is a simple configuration.

Thus our application becomes coupled and highly cohesive.

Points to note about Spring Core Container are

  • It creates object for your application, now object creation is no more headache of your application.
  • What you need is a very straight forward configuration(instruction) file having information about dependencies.
  • You can also configure that whether you want to implement Singleton or not, When you want to create the object i.e. at class loading or when it needed.
  • For implementing Singleton just add one attribute session="singleton". By default all beans are singletons so if you don't want to make it singletonthen give the scope according to your need like session, prototype.
  • If you want to lazily load your objects then give lazy-loading="true". By default it is false.
So now the picture is changed,
here objects are created by container and provided to application when & where needed.

Application : Are you joking ? are you really going to create for me?
Spring Container : Yes my friend, don't create object yourself, I'll create them for you.
Application : Thanks a lot dear, now I can utilize my resources in some other complicated business stuffs.
Spring Container : Welcome !


"Hey ishan I recall that you mentioned one word IoC what is that ? You didn't said anything about that." (question arise in your mind right ??? ;-)

Inversion of Control (IoC) and Dependency Injection(DI)
  • As we learnt objects initial state and life cycle are handled by the environment i.e. container and not by the object itself. The code you write must be highly reusable and modular. It must be independent from environment so rather the creating object in side your code, create them outside and load them using setters. This is called IoC.
  • To get the actual object specifically in programming is called the dependency and the process of supply dependency from outside is called DI.
  • So the dependency(object) is being injected in application using IoC in Spring Core Container.

Do some goggling and tell me
what is Hollywood Principle ?

In simple words, The object of particular class is created by container according to the configuration given in the xml file and that(object) will be given to application using setter or constructor when and where required.

This way we can have Setter DI, Constructor DI, Interface DI.
  • Setter DI : injecting the object using setter method.
  • Constructor DI : injecting the object using constructor.
  • Interface DI : proxy of interface is being instantiated.
This is a simple introduction of Spring Core Container, Inversion of Control and Dependency Injection. There is lot more to learn, but with above knowledge we can start our development.

Friends very soon we'll meet again with Object Compositions in spring where we can use above DIes and also with a web application.


Spring is there to satisfy all our needs, lets celebrate with your favourite bear and apple juice of mine ;-) and of course the cake is ready so lets celebrate our first spring program :-)


Thank you,
Ishan Dave

2 comments: