Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

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.
 
Did Steve tell you that? Fuh - Steve. Just look at this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic