Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Gayatri Ganesh
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • 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
 
Reid M. Pinchback
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
  • 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.
 
Ken Krebs
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the source code for ResourceBundleViewResolver for some ideas.
 
priyabrat mohapatra
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic