Class BeanFactory

  • All Implemented Interfaces:

    public class BeanFactory
    extends java.lang.Object
    implements java.util.Observer
    The BeanFactory 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 the newBean 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 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:

             <bean-mapping bean-name="Bean1" bean-class="test.pkg1.Bean1"/>
             <bean-package package-name="misc.dummypkg2"/>
             <bean-package package-name="misc.dummypkg4" local-factory="DummyPkg4Factory"/>
    The XML above adds one new mapping, from "Bean1" to test.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, the BeanFactory will delegate to an instance of that package's factory, misc.dummypkg4.DummyPkg4Factory.

    A package-level bean factory class specified via the local-factory attribute must meet the following requirements:
    1. It must implement the LocalBeanFactory interface. (See AbstractLocalBeanFactory for a default implementation of this interface.)
    2. It must contain a method,
      public static <return-type> getInstance();
      that returns an instance of the factory class.
    3. The newBean method implementation must be thread-safe.
    See the documentation for more information on customizing beanFactoryConfig.xml.

    Note: BeanFactory is a singleton, so any instance-level setters take effect globally.
    MicroStrategy Web 9.0.0
    • Method Detail

      • getInstance

        public static BeanFactory getInstance()
        Return a BeanFactory instance.
        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 other BeanFactory methods.
        Specified by:
        update in interface java.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 other BeanFactory 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.)
        configPath - The path to the configuration file, relative to the application root directory (ex: "/WEB-INF/xml/config/beanFactoryConfig.xml").
        java.lang.IllegalArgumentException - If configPath is null.
      • getConfigurationPath

        public java.lang.String getConfigurationPath()
        Get the path for the BeanFactory configuration file.
        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,
        Create a bean based on the bean type name.
        beanName - The type name of the desired bean (ex: "ReportBean").
        A new WebComponent instance that corresponds to beanName.
        java.lang.IllegalArgumentException - If beanName is null or empty.
        BeanNotFoundException - If beanName could not be resolved to a class.
        MSTRUncheckedException - If an error occurred in the process of instantiating the bean instance.