Package com.microstrategy.web.beans
Class BeanFactory
- java.lang.Object
-
- com.microstrategy.web.beans.BeanFactory
-
- All Implemented Interfaces:
java.util.Observer
public class BeanFactory extends java.lang.Object implements java.util.Observer
TheBeanFactory
class allows the user to create both MicroStrategy beans and custom beans. It is used by the MicroStrategy Web application to create beans specified in the page configuration files.
Clients create beans using thenewBean
method, which takes a bean type name and retuns an instance of the corresponding bean class. To map a bean name to a bean class, the factory uses a set of bean mappings and an ordered list of packages. A bean mapping maps a bean name (ex: "ReportBean") to a fully qualified classname. The package list provides a set of packages that will be searched in the event that a given bean name isn't explicitly mapped to a class.
Specifically, the logic for creating a bean based on the type name is as follows:- If the bean name is explicitly mapped to a class, instantiate and return an instance of the mapped-to class.
- Otherwise, search the package list in order. For each package:
- If there is a package-level factory associated with the package, attempt
to create the bean by delegating to the factory (a
LocalBeanFactory
instance). - Otherwise, search the package for a class with a name of the form
"<bean name> + Impl"
. If such a class exists, instantiate and return a new instance.
- If there is a package-level factory associated with the package, attempt
to create the bean by delegating to the factory (a
- If the bean name was not explictly mapped and a corresponding class could
not be found in any of the packages in the list, throw a
BeanNotFoundException
.
The list of mappings and packages is populated from an XML configuration file, beanFactoryConfig.xml. Users can use the customization infrastructure to augment or modify the set of mappings and packages. A custom beanFactoryConfig.xml might look like this:
<beanfactory-config> <bean-mappings> <bean-mapping bean-name="Bean1" bean-class="test.pkg1.Bean1"/> </bean-mappings> <bean-packages> <bean-package package-name="misc.dummypkg2"/> <bean-package package-name="misc.dummypkg4" local-factory="DummyPkg4Factory"/> </bean-packages> </beanfactory-config>
The XML above adds one new mapping, from "Bean1" totest.pkg1.Bean1
, to those that already exist. It also appends two packages to the list of packages to search. To create beans in the second package,misc.dummypkg4
, theBeanFactory
will delegate to an instance of that package's factory,misc.dummypkg4.DummyPkg4Factory
.
A package-level bean factory class specified via thelocal-factory
attribute must meet the following requirements:- It must implement the
LocalBeanFactory
interface. (SeeAbstractLocalBeanFactory
for a default implementation of this interface.) - It must contain a method,
public static <return-type> getInstance();
that returns an instance of the factory class. - The
newBean
method implementation must be thread-safe.
Note:BeanFactory
is a singleton, so any instance-level setters take effect globally.- Since:
- MicroStrategy Web 9.0.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BeanFactory.BeanFactoryInfo
static class
BeanFactory.BeanMappingInfo
static class
BeanFactory.BeanMappingInfoList
static class
BeanFactory.BeanPackageInfo
static class
BeanFactory.BeanPackageInfoList
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
destroy()
Destroy the HashSet and other caches, will be called when destroying the sevletjava.lang.String
getConfigurationPath()
Get the path for theBeanFactory
configuration file.static BeanFactory
getInstance()
Return a BeanFactory instance.WebComponent
newBean(java.lang.String beanName)
Create a bean based on the bean type name.void
setConfigurationPath(java.lang.String configPath)
Specify the path for the application's beanFactoryConfig.xml file, relative to the root of the application.void
update(java.util.Observable o, java.lang.Object arg)
Exposed so that this class can update its state when customizations change.
-
-
-
Method Detail
-
getInstance
public static BeanFactory getInstance()
Return a BeanFactory instance.- Returns:
- BeanFactory A BeanFactory instance.
-
update
public void update(java.util.Observable o, java.lang.Object arg)
Exposed so that this class can update its state when customizations change. Clients should not call this method directly. This method is not threadsafe with respect to otherBeanFactory
methods.- Specified by:
update
in interfacejava.util.Observer
-
destroy
public void destroy()
Destroy the HashSet and other caches, will be called when destroying the sevlet
-
setConfigurationPath
public void setConfigurationPath(java.lang.String configPath)
Specify the path for the application's beanFactoryConfig.xml file, relative to the root of the application. This method should be called when the application is initialized, before any instances of this class are used. It is not threadsafe with respect to otherBeanFactory
instance methods. The value of this property is by default an empty string, which means that no application-level configuration file will be used. (In this case, the factory will contain no bean mappings and a single package:com.microstrategy.web.beans
.)- Parameters:
configPath
- The path to the configuration file, relative to the application root directory (ex: "/WEB-INF/xml/config/beanFactoryConfig.xml").- Throws:
java.lang.IllegalArgumentException
- IfconfigPath
is null.
-
getConfigurationPath
public java.lang.String getConfigurationPath()
Get the path for theBeanFactory
configuration file.- Returns:
- The path to the configuration file, relative to the application root directory (ex: "/WEB-INF/xml/config/beanFactoryConfig.xml").
-
newBean
public WebComponent newBean(java.lang.String beanName) throws java.lang.IllegalArgumentException, BeanNotFoundException, MSTRUncheckedException
Create a bean based on the bean type name.- Parameters:
beanName
- The type name of the desired bean (ex: "ReportBean").- Returns:
- A new
WebComponent
instance that corresponds tobeanName
. - Throws:
java.lang.IllegalArgumentException
- IfbeanName
is null or empty.BeanNotFoundException
- IfbeanName
could not be resolved to a class.MSTRUncheckedException
- If an error occurred in the process of instantiating the bean instance.
-
-