Java Doc for SingletonBeanFactoryLocator.java in  » J2EE » spring-framework-2.0.6 » org » springframework » beans » factory » access » 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.access 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   org.springframework.beans.factory.access.SingletonBeanFactoryLocator

All known Subclasses:   org.springframework.context.access.ContextSingletonBeanFactoryLocator,
SingletonBeanFactoryLocator
public class SingletonBeanFactoryLocator implements BeanFactoryLocator(Code)

Keyed-singleton implementation of BeanFactoryLocator, which accesses shared Spring factory instances.

Please see the warning in BeanFactoryLocator's javadoc about appropriate usage of singleton style BeanFactoryLocator implementations. It is the opinion of the Spring team that the use of this class and similar classes is unnecessary except (sometimes) for a small amount of glue code. Excessive usage will lead to code that is more tightly coupled, and harder to modify or test.

In this implementation, a BeanFactory is built up from one or more XML definition file fragments, accessed as resources. The default resource name searched for is 'classpath*:beanRefFactory.xml', with the Spring-standard 'classpath*:' prefix ensuring that if the classpath contains multiple copies of this file (perhaps one in each component jar) they will be combined. To override the default resource name, instead of using the no-arg SingletonBeanFactoryLocator.getInstance() method, use the SingletonBeanFactoryLocator.getInstance(String selector) variant, which will treat the 'selector' argument as the resource name to search for.

The purpose of this 'outer' BeanFactory is to create and hold a copy of one or more 'inner' BeanFactory or ApplicationContext instances, and allow those to be obtained either directly or via an alias. As such, this class provides both singleton style access to one or more BeanFactories/ApplicationContexts, and also a level of indirection, allowing multiple pieces of code, which are not able to work in a Dependency Injection fashion, to refer to and use the same target BeanFactory/ApplicationContext instance(s), by different names.

Consider an example application scenario:

  • com.mycompany.myapp.util.applicationContext.xml - ApplicationContext definition file which defines beans for 'util' layer.
  • com.mycompany.myapp.dataaccess-applicationContext.xml - ApplicationContext definition file which defines beans for 'data access' layer. Depends on the above.
  • com.mycompany.myapp.services.applicationContext.xml - ApplicationContext definition file which defines beans for 'services' layer. Depends on the above.

In an ideal scenario, these would be combined to create one ApplicationContext, or created as three hierarchical ApplicationContexts, by one piece of code somewhere at application startup (perhaps a Servlet filter), from which all other code in the application would flow, obtained as beans from the context(s). However when third party code enters into the picture, things can get problematic. If the third party code needs to create user classes, which should normally be obtained from a Spring BeanFactory/ApplicationContext, but can handle only newInstance() style object creation, then some extra work is required to actually access and use object from a BeanFactory/ApplicationContext. One solutions is to make the class created by the third party code be just a stub or proxy, which gets the real object from a BeanFactory/ApplicationContext, and delegates to it. However, it is is not normally workable for the stub to create the BeanFactory on each use, as depending on what is inside it, that can be an expensive operation. Additionally, there is a fairly tight coupling between the stub and the name of the definition resource for the BeanFactory/ApplicationContext. This is where SingletonBeanFactoryLocator comes in. The stub can obtain a SingletonBeanFactoryLocator instance, which is effectively a singleton, and ask it for an appropriate BeanFactory. A subsequent invocation (assuming the same class loader is involved) by the stub or another piece of code, will obtain the same instance. The simple aliasing mechanism allows the context to be asked for by a name which is appropriate for (or describes) the user. The deployer can match alias names to actual context names.

Another use of SingletonBeanFactoryLocator, is to demand-load/use one or more BeanFactories/ApplicationContexts. Because the definiiton can contain one of more BeanFactories/ApplicationContexts, which can be independent or in a hierarchy, if they are set to lazy-initialize, they will only be created when actually requested for use.

Given the above-mentioned three ApplicationContexts, consider the simplest SingletonBeanFactoryLocator usage scenario, where there is only one single beanRefFactory.xml definition file:

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans_2_0.dtd">
 <beans>
 <bean id="com.mycompany.myapp"
 class="org.springframework.context.support.ClassPathXmlApplicationContext">
 <constructor-arg>
 <list>
 <value>com/mycompany/myapp/util/applicationContext.xml</value>
 <value>com/mycompany/myapp/dataaccess/applicationContext.xml</value>
 <value>com/mycompany/myapp/dataaccess/services.xml</value>
 </list>
 </constructor-arg>
 </bean>
 </beans>
 
The client code is as simple as:
 BeanFactoryLocator bfl = SingletonBeanFactoryLocator.getInstance();
 BeanFactoryReference bf = bfl.useBeanFactory("com.mycompany.myapp");
 // now use some bean from factory 
 MyClass zed = bf.getFactory().getBean("mybean");
 
Another relatively simple variation of the beanRefFactory.xml definition file could be:
<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans_2_0.dtd">
 <beans>
 <bean id="com.mycompany.myapp.util" lazy-init="true"
 class="org.springframework.context.support.ClassPathXmlApplicationContext">
 <constructor-arg>
 <value>com/mycompany/myapp/util/applicationContext.xml</value>
 </constructor-arg>
 </bean>
 <!-- child of above -->
 <bean id="com.mycompany.myapp.dataaccess" lazy-init="true"
 class="org.springframework.context.support.ClassPathXmlApplicationContext">
 <constructor-arg>
 <list><value>com/mycompany/myapp/dataaccess/applicationContext.xml</value></list>
 </constructor-arg>
 <constructor-arg>
 <ref bean="com.mycompany.myapp.util"/>
 </constructor-arg>
 </bean>
 <!-- child of above -->
 <bean id="com.mycompany.myapp.services" lazy-init="true"
 class="org.springframework.context.support.ClassPathXmlApplicationContext">
 <constructor-arg>
 <list><value>com/mycompany/myapp/dataaccess.services.xml</value></value>
 </constructor-arg>
 <constructor-arg>
 <ref bean="com.mycompany.myapp.dataaccess"/>
 </constructor-arg>
 </bean>
 <!-- define an alias -->
 <bean id="com.mycompany.myapp.mypackage"
 class="java.lang.String">
 <constructor-arg>
 <value>com.mycompany.myapp.services</value>
 </constructor-arg>
 </bean>
 </beans>
 

In this example, there is a hierarchy of three contexts created. The (potential) advantage is that if the lazy flag is set to true, a context will only be created if it's actually used. If there is some code that is only needed some of the time, this mechanism can save some resources. Additionally, an alias to the last context has been created. Aliases allow usage of the idiom where client code asks for a context with an id which represents the package or module the code is in, and the actual definition file(s) for the SingletonBeanFactoryLocator maps that id to a real context id.

A final example is more complex, with a beanRefFactory.xml for every module. All the files are automatically combined to create the final definition.

beanRefFactory.xml file inside jar for util module:

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans_2_0.dtd">
 <beans>
 <bean id="com.mycompany.myapp.util" lazy-init="true"
 class="org.springframework.context.support.ClassPathXmlApplicationContext">
 <constructor-arg>
 <value>com/mycompany/myapp/util/applicationContext.xml</value>
 </constructor-arg>
 </bean>
 </beans>
 
beanRefFactory.xml file inside jar for data-access module:
<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans_2_0.dtd">
 <beans>
 <!-- child of util -->
 <bean id="com.mycompany.myapp.dataaccess" lazy-init="true"
 class="org.springframework.context.support.ClassPathXmlApplicationContext">
 <constructor-arg>
 <list><value>com/mycompany/myapp/dataaccess/applicationContext.xml</value></list>
 </constructor-arg>
 <constructor-arg>
 <ref bean="com.mycompany.myapp.util"/>
 </constructor-arg>
 </bean>
 </beans>
 
beanRefFactory.xml file inside jar for services module:
<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans_2_0.dtd">
 <beans>
 <!-- child of data-access -->
 <bean id="com.mycompany.myapp.services" lazy-init="true"
 class="org.springframework.context.support.ClassPathXmlApplicationContext">
 <constructor-arg>
 <list><value>com/mycompany/myapp/dataaccess/services.xml</value></list>
 </constructor-arg>
 <constructor-arg>
 <ref bean="com.mycompany.myapp.dataaccess"/>
 </constructor-arg>
 </bean>
 </beans>
 
beanRefFactory.xml file inside jar for mypackage module. This doesn't create any of its own contexts, but allows the other ones to be referred to be a name known to this module:
<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans_2_0.dtd">
 <beans>
 <!-- define an alias for "com.mycompany.myapp.services" -->
 <alias name="com.mycompany.myapp.services" alias="com.mycompany.myapp.mypackage"/>
 </beans>
 

author:
   Colin Sampaleanu
author:
   Juergen Hoeller
See Also:   org.springframework.context.access.ContextSingletonBeanFactoryLocator
See Also:   org.springframework.context.access.DefaultLocatorFactory


Field Summary
final protected static  Loglogger
    

Constructor Summary
protected  SingletonBeanFactoryLocator(String resourceLocation)
     Constructor which uses the the specified name as the resource name of the definition file(s).

Method Summary
protected  BeanFactorycreateDefinition(String resourceLocation, String factoryKey)
     Actually creates definition in the form of a BeanFactory, given a resource name which supports standard Spring resource prefixes ('classpath:', 'classpath*:', etc.) This is split out as a separate method so that subclasses can override the actual type used (to be an ApplicationContext, for example).

The default implementation simply builds a org.springframework.beans.factory.support.DefaultListableBeanFactory and populates it using an org.springframework.beans.factory.xml.XmlBeanDefinitionReader .

This method should not instantiate any singletons.

protected  voiddestroyDefinition(BeanFactory groupDef, String selector)
     Destroy definition in separate method so subclass may work with other definition types.
public static  BeanFactoryLocatorgetInstance()
     Returns an instance which uses the default "classpath*:beanRefFactory.xml", as the name of the definition file(s).
public static  BeanFactoryLocatorgetInstance(String selector)
     Returns an instance which uses the the specified selector, as the name of the definition file(s).
protected  voidinitializeDefinition(BeanFactory groupDef)
     Instantiate singletons and do any other normal initialization of the factory.
public  BeanFactoryReferenceuseBeanFactory(String factoryKey)
    

Field Detail
logger
final protected static Log logger(Code)




Constructor Detail
SingletonBeanFactoryLocator
protected SingletonBeanFactoryLocator(String resourceLocation)(Code)
Constructor which uses the the specified name as the resource name of the definition file(s).
Parameters:
  resourceLocation - the Spring resource location to use(either a URL or a "classpath:" / "classpath*:" pseudo URL)




Method Detail
createDefinition
protected BeanFactory createDefinition(String resourceLocation, String factoryKey)(Code)
Actually creates definition in the form of a BeanFactory, given a resource name which supports standard Spring resource prefixes ('classpath:', 'classpath*:', etc.) This is split out as a separate method so that subclasses can override the actual type used (to be an ApplicationContext, for example).

The default implementation simply builds a org.springframework.beans.factory.support.DefaultListableBeanFactory and populates it using an org.springframework.beans.factory.xml.XmlBeanDefinitionReader .

This method should not instantiate any singletons. That function is performed by SingletonBeanFactoryLocator.initializeDefinition initializeDefinition() , which should also be overridden if this method is.
Parameters:
  resourceLocation - the resource location for this factory group
Parameters:
  factoryKey - the bean name of the factory to obtain the corresponding BeanFactory reference




destroyDefinition
protected void destroyDefinition(BeanFactory groupDef, String selector)(Code)
Destroy definition in separate method so subclass may work with other definition types.
Parameters:
  groupDef - the factory returned by SingletonBeanFactoryLocator.createDefinition createDefinition()
Parameters:
  selector - the resource location for this factory group



getInstance
public static BeanFactoryLocator getInstance() throws BeansException(Code)
Returns an instance which uses the default "classpath*:beanRefFactory.xml", as the name of the definition file(s). All resources returned by calling the current thread's context classloader's getResources() method with this name will be combined to create a definition, which is just a BeanFactory. the corresponding BeanFactoryLocator instance
throws:
  BeansException - in case of factory loading failure



getInstance
public static BeanFactoryLocator getInstance(String selector) throws BeansException(Code)
Returns an instance which uses the the specified selector, as the name of the definition file(s). In the case of a name with a Spring 'classpath*:' prefix, or with no prefix, which is treated the same, the current thread's context classloader's getResources() method will be called with this value to get all resources having that name. These resources will then be combined to form a definition. In the case where the name uses a Spring 'classpath:' prefix, or a standard URL prefix, then only one resource file will be loaded as the definition.
Parameters:
  selector - the name of the resource(s) which will be read andcombined to form the definition for the BeanFactoryLocator instance.Any such files must form a valid BeanFactory definition. the corresponding BeanFactoryLocator instance
throws:
  BeansException - in case of factory loading failure



initializeDefinition
protected void initializeDefinition(BeanFactory groupDef)(Code)
Instantiate singletons and do any other normal initialization of the factory. Subclasses that override SingletonBeanFactoryLocator.createDefinition createDefinition() should also override this method.
Parameters:
  groupDef - the factory returned by SingletonBeanFactoryLocator.createDefinition createDefinition()



useBeanFactory
public BeanFactoryReference useBeanFactory(String factoryKey) throws BeansException(Code)



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.