• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

multiple instance of a bean

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I made a common Manager and a common Bean to access different tables of my DB (using hibernate).
In a controller, i'm using @Autowired & @Qualifier to automaticly get my bean.

It works pretty well, but now, i'm facing a problem.

I need multiple instance of the common Manager in my controller.
Here is my code :


Here is a part of my springmvc-servlet.xml


The thing is : tlocationManager, tcommManager and initiativManager are the SAME object !
I need it to be different instances..

I searched on the web and found out that you add to a bean definition an attribute or a property singleton="false".
Unfortunately, i tryed it and i've got an error saying this attribute/property is not valid.

How can i solve it ?
Your help would be really appreciated !
 
ranger
Posts: 17347
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 you can define three different beans with different ids, but the same class, then qualify each instance variable to each of the three beans.

You can also try <bean scope="prototype"

adding scope="prototype" to your bean definition and that might work.

Why do you need three? Does the bean hold any state, if not, one bean should be plenty to support as many clients using it at the same time.

Mark>
 
Michael Houlo
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Spritzler wrote:Well you can define three different beans with different ids, but the same class, then qualify each instance variable to each of the three beans.

You can also try <bean scope="prototype"

adding scope="prototype" to your bean definition and that might work.

Why do you need three? Does the bean hold any state, if not, one bean should be plenty to support as many clients using it at the same time.

Mark>



The CommonManager instanciate a CommonDao (using Hibernate). The table reached by the DAO depends of the class used to define the CommonManager.
Eg: private CommonManager<Initiative> initiativManager; => the methods getList() or getObjectById(long ident ) of my DAO will reach my Initiative table.
That is why i need three instances... However, since the CommonManager instanciate the CommonDao using @Autowired, the same instance of the bean CommonDao is also used.. So, generating three beans of the same class CommonManager won't resolve the problem since it'll still uses the same CommonDao.

I resolved my problem by using this :
In my Controller :


And the class CommonTypePropertyEditor.java :


It works.
However, i don't understand how come it's possible to use the same instance of an object but with different declarations..
I mean.. I have ONE instance of the CommonManager bean, but once as <TypeLieu>, once as <TypeCommunication> and once with <Initiative>...
Can you explain this ?

Still, i'm going to try your solution.

Thanks a lot for your reply and sorry if my english isn't that good :P
reply
    Bookmark Topic Watch Topic
  • New Topic