spring-framework / org.springframework.beans.factory.config / ServiceLocatorFactoryBean

ServiceLocatorFactoryBean

open class ServiceLocatorFactoryBean : FactoryBean<Any>, BeanFactoryAware, InitializingBean

A FactoryBean implementation that takes an interface which must have one or more methods with the signatures MyType xxx() or MyType xxx(MyIdType id) (typically, MyService getService() or MyService getService(String id)) and creates a dynamic proxy which implements that interface, delegating to an underlying org.springframework.beans.factory.BeanFactory.

Such service locators permit the decoupling of calling code from the org.springframework.beans.factory.BeanFactory API, by using an appropriate custom locator interface. They will typically be used for prototype beans, i.e. for factory methods that are supposed to return a new instance for each call. The client receives a reference to the service locator via setter or constructor injection, to be able to invoke the locator's factory methods on demand. For singleton beans, direct setter or constructor injection of the target bean is preferable.

On invocation of the no-arg factory method, or the single-arg factory method with a String id of null or empty String, if exactly one bean in the factory matches the return type of the factory method, that bean is returned, otherwise a org.springframework.beans.factory.NoSuchBeanDefinitionException is thrown.

On invocation of the single-arg factory method with a non-null (and non-empty) argument, the proxy returns the result of a org.springframework.beans.factory.BeanFactory#getBean(String) call, using a stringified version of the passed-in id as bean name.

A factory method argument will usually be a String, but can also be an int or a custom enumeration type, for example, stringified via toString. The resulting String can be used as bean name as-is, provided that corresponding beans are defined in the bean factory. Alternatively, a custom between service IDs and bean names can be defined.

By way of an example, consider the following service locator interface. Note that this interface is not dependent on any Spring APIs.

package a.b.c; public interface ServiceFactory { public MyService getService(); }

A sample config in an XML-based org.springframework.beans.factory.BeanFactory might look as follows:

<beans> <!-- Prototype bean since we have state --> <bean id="myService" class="a.b.c.MyService" singleton="false"/> <!-- will lookup the above 'myService' bean by *TYPE* --> <bean id="myServiceFactory" class="org.springframework.beans.factory.config.ServiceLocatorFactoryBean"> <property name="serviceLocatorInterface" value="a.b.c.ServiceFactory"/> </bean> <bean id="clientBean" class="a.b.c.MyClientBean"> <property name="myServiceFactory" ref="myServiceFactory"/> </bean> </beans>

The attendant MyClientBean class implementation might then look something like this:

package a.b.c; public class MyClientBean { private ServiceFactory myServiceFactory; // actual implementation provided by the Spring container public void setServiceFactory(ServiceFactory myServiceFactory) { this.myServiceFactory = myServiceFactory; } public void someBusinessMethod() { // get a 'fresh', brand new MyService instance MyService service = this.myServiceFactory.getService(); // use the service object to effect the business logic... } }

By way of an example that looks up a bean by name, consider the following service locator interface. Again, note that this interface is not dependent on any Spring APIs.

package a.b.c; public interface ServiceFactory { public MyService getService (String serviceName); }

A sample config in an XML-based org.springframework.beans.factory.BeanFactory might look as follows:

<beans> <!-- Prototype beans since we have state (both extend MyService) --> <bean id="specialService" class="a.b.c.SpecialService" singleton="false"/> <bean id="anotherService" class="a.b.c.AnotherService" singleton="false"/> <bean id="myServiceFactory" class="org.springframework.beans.factory.config.ServiceLocatorFactoryBean"> <property name="serviceLocatorInterface" value="a.b.c.ServiceFactory"/> </bean> <bean id="clientBean" class="a.b.c.MyClientBean"> <property name="myServiceFactory" ref="myServiceFactory"/> </bean> </beans>

The attendant MyClientBean class implementation might then look something like this:

package a.b.c; public class MyClientBean { private ServiceFactory myServiceFactory; // actual implementation provided by the Spring container public void setServiceFactory(ServiceFactory myServiceFactory) { this.myServiceFactory = myServiceFactory; } public void someBusinessMethod() { // get a 'fresh', brand new MyService instance MyService service = this.myServiceFactory.getService("specialService"); // use the service object to effect the business logic... } public void anotherBusinessMethod() { // get a 'fresh', brand new MyService instance MyService service = this.myServiceFactory.getService("anotherService"); // use the service object to effect the business logic... } }

See ObjectFactoryCreatingFactoryBean for an alternate approach.

Author
Colin Sampaleanu

Author
Juergen Hoeller

Since
1.1.4

See Also
#setServiceLocatorInterface#setServiceMappingsObjectFactoryCreatingFactoryBean

Constructors

<init>

ServiceLocatorFactoryBean()

A FactoryBean implementation that takes an interface which must have one or more methods with the signatures MyType xxx() or MyType xxx(MyIdType id) (typically, MyService getService() or MyService getService(String id)) and creates a dynamic proxy which implements that interface, delegating to an underlying org.springframework.beans.factory.BeanFactory.

Such service locators permit the decoupling of calling code from the org.springframework.beans.factory.BeanFactory API, by using an appropriate custom locator interface. They will typically be used for prototype beans, i.e. for factory methods that are supposed to return a new instance for each call. The client receives a reference to the service locator via setter or constructor injection, to be able to invoke the locator's factory methods on demand. For singleton beans, direct setter or constructor injection of the target bean is preferable.

On invocation of the no-arg factory method, or the single-arg factory method with a String id of null or empty String, if exactly one bean in the factory matches the return type of the factory method, that bean is returned, otherwise a org.springframework.beans.factory.NoSuchBeanDefinitionException is thrown.

On invocation of the single-arg factory method with a non-null (and non-empty) argument, the proxy returns the result of a org.springframework.beans.factory.BeanFactory#getBean(String) call, using a stringified version of the passed-in id as bean name.

A factory method argument will usually be a String, but can also be an int or a custom enumeration type, for example, stringified via toString. The resulting String can be used as bean name as-is, provided that corresponding beans are defined in the bean factory. Alternatively, a custom between service IDs and bean names can be defined.

By way of an example, consider the following service locator interface. Note that this interface is not dependent on any Spring APIs.

package a.b.c; public interface ServiceFactory { public MyService getService(); }

A sample config in an XML-based org.springframework.beans.factory.BeanFactory might look as follows:

<beans> <!-- Prototype bean since we have state --> <bean id="myService" class="a.b.c.MyService" singleton="false"/> <!-- will lookup the above 'myService' bean by *TYPE* --> <bean id="myServiceFactory" class="org.springframework.beans.factory.config.ServiceLocatorFactoryBean"> <property name="serviceLocatorInterface" value="a.b.c.ServiceFactory"/> </bean> <bean id="clientBean" class="a.b.c.MyClientBean"> <property name="myServiceFactory" ref="myServiceFactory"/> </bean> </beans>

The attendant MyClientBean class implementation might then look something like this:

package a.b.c; public class MyClientBean { private ServiceFactory myServiceFactory; // actual implementation provided by the Spring container public void setServiceFactory(ServiceFactory myServiceFactory) { this.myServiceFactory = myServiceFactory; } public void someBusinessMethod() { // get a 'fresh', brand new MyService instance MyService service = this.myServiceFactory.getService(); // use the service object to effect the business logic... } }

By way of an example that looks up a bean by name, consider the following service locator interface. Again, note that this interface is not dependent on any Spring APIs.

package a.b.c; public interface ServiceFactory { public MyService getService (String serviceName); }

A sample config in an XML-based org.springframework.beans.factory.BeanFactory might look as follows:

<beans> <!-- Prototype beans since we have state (both extend MyService) --> <bean id="specialService" class="a.b.c.SpecialService" singleton="false"/> <bean id="anotherService" class="a.b.c.AnotherService" singleton="false"/> <bean id="myServiceFactory" class="org.springframework.beans.factory.config.ServiceLocatorFactoryBean"> <property name="serviceLocatorInterface" value="a.b.c.ServiceFactory"/> </bean> <bean id="clientBean" class="a.b.c.MyClientBean"> <property name="myServiceFactory" ref="myServiceFactory"/> </bean> </beans>

The attendant MyClientBean class implementation might then look something like this:

package a.b.c; public class MyClientBean { private ServiceFactory myServiceFactory; // actual implementation provided by the Spring container public void setServiceFactory(ServiceFactory myServiceFactory) { this.myServiceFactory = myServiceFactory; } public void someBusinessMethod() { // get a 'fresh', brand new MyService instance MyService service = this.myServiceFactory.getService("specialService"); // use the service object to effect the business logic... } public void anotherBusinessMethod() { // get a 'fresh', brand new MyService instance MyService service = this.myServiceFactory.getService("anotherService"); // use the service object to effect the business logic... } }

See ObjectFactoryCreatingFactoryBean for an alternate approach.

Functions

afterPropertiesSet

open fun afterPropertiesSet(): Unit

getObject

open fun getObject(): Any

getObjectType

open fun getObjectType(): Class<*>

isSingleton

open fun isSingleton(): Boolean

setBeanFactory

open fun setBeanFactory(beanFactory: BeanFactory): Unit

setServiceLocatorExceptionClass

open fun setServiceLocatorExceptionClass(serviceLocatorExceptionClass: Class<out Exception>): Unit

Set the exception class that the service locator should throw if service lookup failed. The specified exception class must have a constructor with one of the following parameter types: (String, Throwable) or (Throwable) or (String).

If not specified, subclasses of Spring's BeansException will be thrown, for example NoSuchBeanDefinitionException. As those are unchecked, the caller does not need to handle them, so it might be acceptable that Spring exceptions get thrown as long as they are just handled generically.

setServiceLocatorInterface

open fun setServiceLocatorInterface(interfaceType: Class<*>): Unit

Set the service locator interface to use, which must have one or more methods with the signatures MyType xxx() or MyType xxx(MyIdType id) (typically, MyService getService() or MyService getService(String id)). See the ServiceLocatorFactoryBean for information on the semantics of such methods.

setServiceMappings

open fun setServiceMappings(serviceMappings: Properties): Unit

Set mappings between service ids (passed into the service locator) and bean names (in the bean factory). Service ids that are not defined here will be treated as bean names as-is.

The empty string as service id key defines the mapping for null and empty string, and for factory methods without parameter. If not defined, a single matching bean will be retrieved from the bean factory.