This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Spring and the fly likes Obtaining Spring beans elegantly without polluting classes with spring dependencies Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Obtaining Spring beans elegantly without polluting classes with spring dependencies" Watch "Obtaining Spring beans elegantly without polluting classes with spring dependencies" New topic
Author

Obtaining Spring beans elegantly without polluting classes with spring dependencies

Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Hey,

I am changing the mechanism for handling services to spring beans. now I was wondering how to obtain spring beans in an elegant way. What I do not want is to call



in all of my service consuming classes, as this would pollute these classes with spring dependencies.

I figured the following approach to be more elegant. I was wondering though, if there are any arguments against it.

So here goes:

bean definition


ServiceLocator class




This way I have the dependency to the bean factory centralized in one class and also a central service accessor class.

Looking forward to hear your opinions


JDBCSupport - An easy to use, light-weight JDBC framework -
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

In all my Spring applications, I never need to call getBean. I just use Spring to do dependency injections to inject beans into other beans. At the client level, it depends on the technology that I am using, but even with that all the technologies have some Spring integration to have beans injected into them without you ever calling getBean.

Where were you calling getBean? Can you give us an example>

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
hey mark,

thanks for your reply.

yah, this is the general approach. usually there is some other spring consuming framework that takes care of all the beans.

in this context though, there is no other framework involved in the application. it is a server application based on jboss netty. the architecture is set up to have a service layer. as of now, they are all wired together in a ServiceLocator class, as all of these services are singletons and are instantiated once in a static initializer and that's it.

now though, a service cannot run as a singleton. and that's where i find it's easier to hand this task over to spring.

as for the service consuming classes, these can be message handlers that are triggered by the netty framework that need some services to perform their task.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

Your approach sounds pretty reasonable!! (In the absence of spring framework consuming class!)


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Kevin Cho
Greenhorn

Joined: Nov 12, 2008
Posts: 20
I'm a bit confused...

I assume you created that class to centralize Spring dependency. If so... I guess that's fine but why not utilize Sun's standar JSR250 and 330.

I forget the details but you can use JEE5 annotations instead of using Spring Annogations. Maybe I misunderstood your question.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Obtaining Spring beans elegantly without polluting classes with spring dependencies
 
Similar Threads
Spring Junit test issue
ClassPathXmlApplicationContext wildcard problem
Not able to understant object initialisation
Injecting a Dependency through Spring
Can I read Spring configuration twice in my applction?