This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

how to make spring aware of bean initialization

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
zabet tyan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the reason that you cannot use DI?
 
zabet tyan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic