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 how to make spring aware of bean initialization 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 "how to make spring aware of bean initialization" Watch "how to make spring aware of bean initialization" New topic
Author

how to make spring aware of bean initialization

zabet tyan
Ranch Hand

Joined: Jan 14, 2009
Posts: 32
Hi,

I have a class A registered in my ApplicationContext.xml.

At some point, class A creates object B,


b has dependend on bean C which is injected into it.

How do I make Spring aware of the fact that B was created so that it can inject the required dependencies?

I thought of making my A class BeanFactoryAware, but that doesn't sound like a good idea.

Note that I might have to create several B objects that must be initialized.

I cannot inject class B to class A.

Thanks
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

You could create Factory beans and inject that into your A class, then call a method on it to create the B.

Just a thought of the top of my head, sorry I didn't think too hard on it for too long.

The main point with Spring and dependency injection is for you not to call "new"

there are beans that you can create and in the configuration set a factory-method or an init-method that it will call.

Look at the FactoryBean interface too to see if that might help.

If not, and you come across a situation that can't be resolved, look closely at the design there is usually a problem there that causes the tough situation. That is my motto.

Mark


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

Joined: Jan 14, 2009
Posts: 32
Thanks Mark,


Yes I am porting my project over from jboos/ejb to tomcat/spring so need to do a lot of design reviews regardless i guess. thought maybe spring has some quick and easy fix so I don't have to work too hard...
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Well there is a Spring class that you can have your EJBs extend that would give them access to the ApplicationContext, but it then couples your EJB to a Spring specific class.

Mark
zabet tyan
Ranch Hand

Joined: Jan 14, 2009
Posts: 32

Well I am running away from EJBs. So don't want to keep any EJBs around. Anyways, I redesigned that section, but had to have one of my class implement BeanFactoryAware.
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
What is the reason that you cannot use DI?


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
zabet tyan
Ranch Hand

Joined: Jan 14, 2009
Posts: 32
There is a config file that contains some information. Depending on some value in config file, say x = 4, I have to create 4 B objects.

Right now I set object B to have prototype scope in spring config and use beanFactoryAware to get the B object. Object B depends on object C and needs spring to inject it.
 
 
subject: how to make spring aware of bean initialization
 
Similar Threads
Spring 3 and Hibernate
How to inject EntityManager via setter in spring
spring wont inject the entitymanager
nonintrusive in spring
Transactions in Spring + Hibernate