• 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

Implementing factory pattern with autowiring in spring

 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I need to implement factory pattern in spring, until now i was using spring config file. The issue is that i have Dao Interface and have two concrete implementation one is solr and the othe is mockDao

I have put the snippet below for my config file and it works fine, what i wanted was how to use the ObjectFactoryCreatingFactoryBean with annotation
As seen below i have a daoFactory class as singleton with map defined with One key referencing to mockDaoFactory which is "ObjectFactoryCreatingFactoryBean" and its target is hooked up to the concreate implementation. this gives me flexibility for make Daofactory return different implementaion of concreate classes.
i am sure this kind of map entries can be defined using annotation too, can someone please let me know about any tutorial etc...


Much appreciated
Thanks
Rashid
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming that both mockDaoImpl and mockDaoSolr implement the same interface, you could use the following autowiring to set all concrete implementations in a Map. The problem is that the Map's key will be the name of the bean.







The Main class execution will produce something like :
{mockDaoImpl=x.y.DaoMockImpl@c7b00c, mockDaoSolr=x.y.DaoSolrImpl@1f6f296}
 
Darvesh Niz
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply
the other thing i wanted to ask was how to tell the DaoFactory to be singleton. In the config file we can always specify the scope as singleton or prototype, but when we use autowire how to we tell spring to create singleton beans or not.

Thanks for your suggestion.
the only concern i have in your prescribed example, you are setting the map with concrete dao implementaion class, but the issue i that, map will have reference to only one object of either solrDaoImpl or mockDaoImpl.

the issue is that how can i get a new Dao concrete instance whenever i call DaoFactory.getconcrete class implementation.
the previous example i quoted was that my dao factory was wired to objectProxyFactoryBean which will give new instances of concrete implementation.
Hope i am making sense

thanks
Rashid
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, I'm a bit out of sync Mmmhh... I can't think of a better way to use the ObjectFactoryCreatingFactoryBean. I'm not sure autowiring will help here.
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic