Java Doc for ServiceLocatorFactoryBean.java in  » J2EE » spring-framework-2.0.6 » org » springframework » beans » factory » config » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » spring framework 2.0.6 » org.springframework.beans.factory.config 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   org.springframework.beans.factory.config.ServiceLocatorFactoryBean

ServiceLocatorFactoryBean
public class ServiceLocatorFactoryBean implements FactoryBean,BeanFactoryAware,InitializingBean(Code)
A org.springframework.beans.factory.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, ServiceLocatorFactoryBean.setServiceMappings(java.util.Properties) a custom mapping 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 dependant 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 dependant 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:   ServiceLocatorFactoryBean.setServiceLocatorInterface
See Also:   ServiceLocatorFactoryBean.setServiceMappings
See Also:   ObjectFactoryCreatingFactoryBean





Method Summary
public  voidafterPropertiesSet()
    
protected  ExceptioncreateServiceLocatorException(Constructor exceptionConstructor, BeansException cause)
     Create a service locator exception for the given cause.
protected  ConstructordetermineServiceLocatorExceptionConstructor(Class exceptionClass)
     Determine the constructor to use for the given service locator exception class.
public  ObjectgetObject()
    
public  ClassgetObjectType()
    
public  booleanisSingleton()
    
public  voidsetBeanFactory(BeanFactory beanFactory)
    
public  voidsetServiceLocatorExceptionClass(Class serviceLocatorExceptionClass)
     Set the exception class that the service locator should throw if service lookup failed.
public  voidsetServiceLocatorInterface(Class interfaceType)
     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)).
public  voidsetServiceMappings(Properties serviceMappings)
     Set mappings between service ids (passed into the service locator) and bean names (in the bean factory).



Method Detail
afterPropertiesSet
public void afterPropertiesSet()(Code)



createServiceLocatorException
protected Exception createServiceLocatorException(Constructor exceptionConstructor, BeansException cause)(Code)
Create a service locator exception for the given cause. Only called in case of a custom service locator exception.

The default implementation can handle all variations of message and exception arguments.
Parameters:
  exceptionConstructor - the constructor to use
Parameters:
  cause - the cause of the service lookup failure the service locator exception to throw
See Also:   ServiceLocatorFactoryBean.setServiceLocatorExceptionClass




determineServiceLocatorExceptionConstructor
protected Constructor determineServiceLocatorExceptionConstructor(Class exceptionClass)(Code)
Determine the constructor to use for the given service locator exception class. Only called in case of a custom service locator exception.

The default implementation looks for a constructor with one of the following parameter types: (String, Throwable) or (Throwable) or (String).
Parameters:
  exceptionClass - the exception class the constructor to use
See Also:   ServiceLocatorFactoryBean.setServiceLocatorExceptionClass




getObject
public Object getObject()(Code)



getObjectType
public Class getObjectType()(Code)



isSingleton
public boolean isSingleton()(Code)



setBeanFactory
public void setBeanFactory(BeanFactory beanFactory) throws BeansException(Code)



setServiceLocatorExceptionClass
public void setServiceLocatorExceptionClass(Class serviceLocatorExceptionClass)(Code)
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.
See Also:   ServiceLocatorFactoryBean.determineServiceLocatorExceptionConstructor(Class)
See Also:   ServiceLocatorFactoryBean.createServiceLocatorException(java.lang.reflect.Constructor,org.springframework.beans.BeansException)
See Also:   org.springframework.beans.BeansException
See Also:   org.springframework.beans.factory.NoSuchBeanDefinitionException




setServiceLocatorInterface
public void setServiceLocatorInterface(Class interfaceType)(Code)
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 class-level Javadoc for information on the semantics of such methods.
Parameters:
  interfaceType - the java.lang.Class of the interface to be used for the service locator



setServiceMappings
public void setServiceMappings(Properties serviceMappings)(Code)
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.
Parameters:
  serviceMappings - mappings between service ids and bean names,with service ids as keys as bean names as values




Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.