Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to make spring aware of bean initialization

 
zabet tyan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic