Pages

Sunday, July 7, 2013

The Four Pearls of Spring-1

From the previous post we know spring makes development a kids play.One of  the four strategies which make it do so will be looked at in the post.

Pearl 1: POJO
With an awesome acronym like that you are bound to do awesome things. So, the awesome thing that POJO does is, it does barely anything, other than what you really want it to do!! Now that to you would seem odd. Let me show you a POJO in EJB2.0 first

package com.perius.ejb.session;

import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

public class HelloWorldBean implements SessionBean{

public void ejbActivate()
{

}

public void ejbPassivate()
{

{

public void ejbRemove()
{

}
//couple of more such interface methods to implement which I wont here..

public String sayHello()
{
 return "Hello World!!";
}

}

This is a Bean in EJB2.0 which only has the only function of returning the string "Hello World" everytime it is created and sayHello method is invoked. But we had to implement 4 more methods which would let the bean to hook to the session bean lifecycle . This is forceful requirement. Even I as developer who doesn't want to write all those stupid methods which have nothing to do with my real business logic(Saying hello world) would have to implement them. This is dictatorship!! I hate it. It is like I made a mistake choosing EJB2 for it doesnt work for me. I work for it!

Now our darling Spring avoids this stupid mess by never making me implement spring specific interfaces or extend spring specific classes.(In new spring you have annotations, but still my java code remains) So my beautiful little bean can be used anywhere as it is not bound to spring by any method or interface making it reusable and don't we developers love that!!

So my spring code for the same hellobean would look somewhat like this:

package com.perius.spring;

public class HelloWorldBean(

public String sayHello()
{
    return "Hello World";
}

And shouldn't my bean look like that a plain old java object aka the POJO!!

No comments:

Post a Comment