Friday, December 24, 2010

What is Spring, really?

I should preface this with this statement.  I actually do like Spring.  At least Spring core.  And I use it almost every chance I get.

But I started to really think about it the other day.  What is Spring?  What is it doing?  What is dependency injection?

Here is a typical usage.  At least from my experience.

class SomeService {
    @Autowired
    SomeDao someDao;
}


So whats going on.  I'll tell you a dirty little secret.  SomeDao is a reference to a Global variable.  But here in OO land Global is an obscenity.

So what do we do to hide the fact that we are using Global variables but want to pretend we are OO kosher.

We use an annotation.  And 5 layers of javASSist.

What did this achieve?  This @Autowired.  Well it saved me from writing

SomeDao someDao = SomeDao.getInstance();

Which would probably be a Singleton pattern.  Which is evil too of course.

Or maybe I would have written it

SomeDao someDao = SomeDaoFactory.getSomeDao();

Factory how retro.  We don't use Factories any more.  They just pollute.

So, now I have a new clean elegant way to get my Globals.

I just use @Autowired

And that makes me feel a lot better.

No comments:

Post a Comment