Tuesday 15 January 2013

java - How to inject different services at runtime based on a property with Spring without XML -



java - How to inject different services at runtime based on a property with Spring without XML -

i using spring boot java standalone application. have bean makes utilize of service. want inject different implementations of service @ runtime, based on property in properties file spring (4 matter).

this sounds mill pattern, spring allows using annotations solve problem, this.

@autowired @qualifier("selectorproperty") private myservice myservice;

then in beans.xml file have alias, can utilize property in @qualifier.

<alias name="${selector.property}" alias="selectorproperty" />

and in different implementations have different qualifiers.

@component("selector1") public class myserviceimpl1 @component("selector2") public class myserviceimpl2

application.properties

selector.property = selector1 selector.property = selector2

whereas regarding mill pattern, in spring can utilize servicelocatorfactorybean create mill give same functionality.

<bean class="org.springframework.beans.factory.config.servicelocatorfactorybean" id="myservicefactory"> <property name="servicelocatorinterface" value="my.company.myservicefactory"> </property> </bean> public interface myservicefactory { myservice getmyservice(string selector); }

and in bean can utilize right implementation @ runtime depending on value of property.

@value("${selector.property}") private string selectorproperty; @autowired private myservicefactory myservicefactory; private myservice myservice; @postconstruct public void postconstruct() { this.myservice = myservicefactory.getmyservice(selectorproperty); }

but problem solution not find way avoid using xml define factory, , utilize annotations.

so question be, there way utilize servicelocatorfactorybean (or equivalent) using annotations, or forced utilize @autowired @qualifier way if not want define beans in xml? or there other way inject different services @ runtime based on property spring 4 avoiding xml? if reply utilize @autowired @qualifier alias, please give reason why improve using known mill pattern.

using xml forcing me utilize @importresource("classpath:beans.xml") in launcher class, i'd rather not utilize either.

thanks.

actually, can utilize servicelocatorfactory without xml declaring bean in configuration file.

@bean public servicelocatorfactorybean myfactoryservicelocatorfactorybean() { servicelocatorfactorybean bean = new servicelocatorfactorybean(); bean.setservicelocatorinterface(myservicefactory.class); homecoming bean; } @bean public myservicefactory myservicefactory() { homecoming (myservicefactory) myfactoryservicelocatorfactorybean().getobject(); }

then can still utilize mill usual, no xml involved.

@value("${selector.property}") private string selectorproperty; @autowired @qualifier("myservicefactory") private myservicefactory myservicefactory; private myservice myservice; @postconstruct public void postconstruct() { this.myservice = myservicefactory.getmyservice(selectorproperty); }

java xml spring spring-boot factory-pattern

No comments:

Post a Comment