aspose file tools*
The moose likes Other Application Frameworks and the fly likes Spring BeanFactory ? can it load depending on a value of a property ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Spring BeanFactory ? can it load depending on a value of a property ? " Watch "Spring BeanFactory ? can it load depending on a value of a property ? " New topic
Author

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

Gayatri Ganesh
Ranch Hand

Joined: Dec 23, 2003
Posts: 143
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

Joined: Jan 25, 2002
Posts: 775
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.


Reid - SCJP2 (April 2002)
Ken Krebs
Ranch Hand

Joined: Nov 27, 2002
Posts: 451
Take a look at the source code for ResourceBundleViewResolver for some ideas.


kktec<br />SCJP, SCWCD, SCJD<br />"What we observe is not nature itself, but nature exposed to our method of questioning." - Werner Heisenberg
priyabrat mohapatra
Greenhorn

Joined: Dec 03, 2008
Posts: 1
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();
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Spring BeanFactory ? can it load depending on a value of a property ?