Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Spring BeanFactory ? can it load depending on a value of a property ?

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

I have a question regarding spring :

i have an interface, MyInterface which has different implementations for each country ie the implementation depends on the country.
MyInterface has implementations like MyInterfaceUSAImpl, MyInterfaceGermanyImpl, etc

In my manager class I want to call
MyInteface.method(country)

when I call above method it should get the correct Impl depending on the country.

I know that the Spring framework has a BeanFactory. Can I tell the spring framework to return me the correct implementation depending on country ?

Can anyone please advice ?

Thanks,
Gayatri
 
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't think there is a *direct* way of doing this, but there is an easy way of doing it.

Make a proxy object with the same interface as your other implementation objects. That proxy object would be responsible for using the usual Java I18N mechanisms for selecting the real implementation it will be a proxy for. Spring's factory creates the proxy and provides whatever initialization parameters you want; your proxy selects the implementation and passes along, where appropriate, the initialization parameters.
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the source code for ResourceBundleViewResolver for some ideas.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A factory class can be a simple solution to this problem.
In this case I will pass country to this factory rather than in the way mentioned in the problem [MyInterface.method(country)].
And after getting an object of correct implementation class I will invoke its methods as myInterface.method(); myInterface being a variable of type MyInterface.

To spring-enable the factory class, will make it implement org.springframework.beans.factory.FactoryBean interface.
Then in my Client code I can have following piece of code to pass different country names to the factory to get object of corresponding implementation class.

// getting an object of factory class. note the & prefix in parameter value
CountryWiseObjectCreator obejctFactory = (CountryWiseObjectCreator)factory.getBean("&obejctFactory");
// setting for which country an object has to be created
objectFactory.setStrCountry(�gusa�h);
// getting object of corresponding implementation class
MyInterface mi = (MyInterface)ObjectFactory.getObject();
// invoking methods of object fetched above.
mi.testMethod();
 
get schwifty. tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic