Springing :D => Something About Dependency Injection

I just got an understanding of what is dependency injection!! I was hearing Dependency Injection from my colleagues and all, and I was stumbled!! what actually it is?   Well now I say that’s a real piece of cake !!!.

The phrase “Dependency Injection” may sound intimidating. But as when we look closer we will realize that it’s not complex as it sounds .And by applying these DesignPattern(DependencyInjection is_a DesignPattern). to your code, then it become significantly easier to understand and easier to extent and test. Am quoting an example from a text  here through which one can easily understand what actually it is.

Consider a Knight class.

  1. package com.springaction.knights;
  2.  public class DamselRescuingKnight implements Knight {
  3.               private RescueDamselQuest quest;
  4.  public DamselRescuingKnight() {
  5.         quest = new RescueDamselQuest();
  6.    }
  7.  public void embarkOnQuest() throws QuestException {
  8.        quest.embark();
  9.    }
  10. }

In the above snippet you can see DamselResuceKnight creates it’s on quest , a RescueDamselQuest(), within the constructor (LOC – 4&5).This actually makes a DamselRescuingKnight tightly coupled to a RescueDamselQuest() and it severly limits the knight’s quest-embarking repertoire. Actually it can rescue a Damsel yea it is supposed to that, but what else it can do? Nothing but simply sit.

And also when you get familiar with testing, you will find it difficult to test such a piece of code.(unitTests). In writing test, the scenario here is you got to assert that quest’s embark(); method is called when the knight’s embarkOnQuest() is called, the code is supposed to do so, But there is no clear way to test that here, so the DamselRescuingKnight will remain untested.

Here comes the Dependency Injection(DI). With DI, objects are given their dependencies at creation time by some third party that coordinates each object in the system. Objects aren’t expected to create or obtain their dependencies – dependencies are injected into the objects that need them.

Illustrating DI with the below code snippet

Consider a class BraveKnight and see that BraveKnight is ready and flexible enough to take any Quest!!

  1. package com.springaction.knights;
  2. public class BraveKnight implements Knight {
  3.    private Quest quest;
  4. public BraveKnight (Quest quest) {
  5.    this.quest = quest;  <————-    //Here the quest is being injected
  6.  }
  7. public void embarkOnQuest() throws QuestException {
  8.   quest.embark();
  9.  }
  10. }

As in the above snippet unlike DamselRescueKnight, BraveKnight doesn’t create his own quest. Instead he is given a quest at the construction time as a constructor argument.This is a type of DI and is known as Constructor Injection.

The quest he’s give as type Quest, is an interface that all quests implement. So the BraveKnight  could embark on a RescueDamselQuest, a SlyDragonQuest , or any other Quest implementation he is given. The point here is that BraveKnight isn’t coupled to any specific implementation of Quest.It doesn’t matter to him what kind of quest he is asked to embark upon.

Something About Hibernate

Hibernate is an Object Relational Mapping solution for Java, and it’s an open-source persistent framework.

It is a powerful, High Performance, Object Relational Persistence Query Service for any Java Application.

How Hibernate Functions:-

  • Hibernate maps Java Classes to Database Types and Java Data types to SQL data types, and thus it makes the life of a programmer easier.
  • Hibernate make use of XML configurations for mapping Java Classes to  database Objects.
  • Provides simple API for accessing Java Objects to and from the database.
  • Changes in the database can be reflected by changing the XML configuration file.
  • It Abstracts the SQL queries & helps us working with Java Objects.
  • It doesn’t require an application server to work.
  •  Can manage complex associations through Objects.
  • Ease in Data Retrieval
  • And It supports almost all RDBMS.
  • It uses existing Java API’s like JDBC ,JNDI, Java TransactionAPI. JDBC provides a rudimentary level of abstraction,and also it allows any database with a JDBC driver can be integrated with Hibernate.
  • JNDI and Java Transaction API (JTI) helps Hibernate to get integrated with J2EE application servers.


Hibernate sits between Traditional Java objects and database server to handle all the work in persisting objects based on appropriate Object-Relational mechanisms and patterns.

A High Level Hibernate Architecture


Objects which gave birth in an Hibernate Application

  • Configuration Object
  • Session Factory Object
  • Session Object
  • Transaction Object
  • Query Object
  • Criteria Object


  • Configuration Object

It’ the first ever object that get created in an Hibernate Application, and it’s created automatically during application initialization process. The two key components of Configuration Objects are :

  1. Database Connection => which is handled by hibernate.properties and hibernate.cfg.xml.
  2. Class Mapping Setup => This helps in creating in connection with Java Classes and DB tables.
  •  Session Factory Object

The above mentioned Configuration Object is used to create Session Factory Object which inturn configures Hibernate for the application with the supplied configuration file => which instantiates a Session Object. Session Factory Object is created during application startup.We will need one SessionFactory Object per Database and vice versa.

  • Session Object

The basic purpose of this object is to get a physical connection with the database & this object will be instantiated each time when database interaction occurs. They are not thread safe( should be destroyed/created as needed).

  • Transaction Object

This is an optional object , and Hibernate applications may choose not to use this interface.

  •   Query Object

Query Objects use SQL or Hibernate Query Language string to retrieve data from the database and create objects.

  • Criteria Object

These objects are used to create and execute object oriented criteria queries to retrieve objects.

I am still at the beginning level of Computer Science!!

And really feeling bad!! as I prepare for the Hacker Earth challenge and suddenly realizing that you are nothing !! As I fail in solving 90% of the problems I tried. With a dream to end, at least in #ThoughtWorks , and reading the book of GayleLaakman, am feeling really disappointed!! Well, its  somewhat late, now I am going to restart!! As now I fixed a target and to demolish each one as the array looks like

[c++, Algorithms, Design Patterns, CCI,].


And this time ,I feels much better!! and Determined, and I can now blame the ecosystem I was brought up, as a computer science student, I never learned these stuffs deeply. Well it was my responsibility to search for that path, and I didn’t and later realizing this path is the ideal one for a computer science student, Now I can see many languages becoming popular day by day, like Ruby, Python, R, GO  etc, –> This is going to be a piece of advise for the upcoming folks, complete traversing my array first and Jump to the below “Ruby Python etc…..”. As I understood, everything lies there, the rest is pretty simple and you can pick up on the way.

Well it always better to be late than never, and let me begin my traversing!! with a dream to step at amazon, google, facebook, etc…

A new experience || blogging from BlackBerry

Well just trying to write some craps!! Or in other words am simply testing the BlackBerry WordPress App! It’s really good and fast too!! UI is designed in a pretty awesome way! I found at certain options app getting a bit laggy, but in short this is a great piece of work!


A screen shot to show how the interface is, well for me this app is really a piece of cake ;)

That’s distracting

Today happened to see!! “the facebook profile again” its all because of the recommendation engine by #Facebook!! huh facebook knows whom I wanna see, but I shouldn’t have looked,as its really making me frustrated and more sad! and I remember that slight mistake from my part if needed can be forgiven, but now it had taken this form and it’s like this in present.I forgot about many other things to worry ;) so right now am leaving this, and I believe something good will happen!! No matter what ever be the situation I will always stand with truth and I will be truthful.and it’s like now its more than 3 or 4 years my struggle with life continues, still the hope that I can bring it, I can make it, is driving me forward, and at some instant I feel am loosing, simultaneously these kind off pop ups also adding a bit to sufferings. well still am keeping my heads up, even if inside is burning!!! Trying to be focused, learning to intake, and learning to let go!!

കലാലയ ജീവിതവും സഫലമാവാത്ത ചില സ്വപ്നങ്ങളും !!!

ഇന്നു  ഞാൻ ഒരു youtube വീഡിയോ  കാണുക  ഉണ്ടായി !! വലിയ  ഡീൽ  ഒന്നുമില്ല , ഇറ്റ്‌  വാസ്  എ “ഫ്ലാഷ് മോബ് “!! അത്  എന്നെ  എന്റെ കലാലയത്തിലേക്ക്  ആണ്  കൊണ്ട്  പോയത് , അവിടുത്തെ ചില ഓർമകൾ !! 
ഒരുപാടു  സ്വപങ്ങളും  ആയി ആണ്  ഞാൻ  അവിടെ  ചെന്ന്  കേറുന്നത് !! >>>>