my dog learned polymorphism
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Dependency Injection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Dependency Injection" Watch "Dependency Injection" New topic

Dependency Injection

Kevin Broderick
Ranch Hand

Joined: Jul 19, 2009
Posts: 39
Hello Ranchers,

I've got to ask ye this so as I can put the record straight. In my business service class instead of doing:

BusinessService(DAODatabase database)

what I've done instead is

BusinessService(String Connection)

where the string connection is currently a file location but in the future can be an sql statement. Using the parameter, it calls on the scheme to get the DAO. Would it be correct to say that my constructor has dependency injection?

Thanks ranchers

Roel De Nijs

Joined: Jul 19, 2004
Posts: 8384

In my understanding your constructor doesn't use dependency injection.

SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
Roberto Perillo

Joined: Dec 28, 2007
Posts: 2271

Howdy, Kevin!

Would it be correct to say that my constructor has dependency injection?

Hum... no, like my good buddy Roel showed above.

Other than that, the second approach you showed is very inflexible. If the data source changes in the future, then you'll have to open your BusinessService class to modify your code so it works correctly. Today, we have to deal with a .db file, but tomorrow, it could be a database. So, the first approach is much better.

If you have a factory or your Data class is a singleton, then you can obtain it, for instance, in the constructor of your BusinessService class. But receiving a String in the business component is not a good idea.

Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
I agree. Here's the link:
subject: Dependency Injection
It's not a secret anymore!