Interface BeanContext

  • All Known Implementing Classes:
    StandaloneBeanContext

    public interface BeanContext
    Bean Context

    This class represents a context of bean's and the means to get and set there instances.

    This class uses a BeanIdStore to optimize the access performance. If all the BeanId objects are registered with the BeanIdStore before this object is created then you get 'direct access' performance.

    For performance reasons it is best to register all BeanId objects up front. Because if new BeanId objects are registered after the BeanContext is created then the BeanContext needs to do synchronize with the BeanIdStore,

    It is possible to get the bean by it's bean id String name. However this isn't as fast as using the BeanId objects.

    Author:
    maurice.zeijen@smies.com
    • Method Detail

      • addBean

        void addBean​(BeanId beanId,
                     Object bean)
        Add a bean instance under the specified BeanId.
        Parameters:
        beanId - The BeanId under which the bean is to be stored.
        bean - The bean instance to be stored.
      • addBean

        void addBean​(BeanId beanId,
                     Object bean,
                     Fragment<?> source)
        Add a bean instance under the specified beanId string.

        Generates a BeanId in the background.

        Parameters:
        beanId - The BeanId under which the bean is to be stored.
        bean - The bean instance to be stored.
        source - Source fragment.
      • addBean

        void addBean​(String beanId,
                     Object bean)
        Add a bean instance under the specified beanId.

        If performance is important, you should get (and cache) a BeanId instance for the beanId String and then use the addBean(BeanId, Object, Fragment) method.

        Parameters:
        beanId - The beanId under which the bean is to be stored.
        bean - The bean instance to be stored.
      • addBean

        void addBean​(String beanId,
                     Object bean,
                     Fragment<?> source)
        Add a bean instance under the specified beanId.

        If performance is important, you should get (and cache) a BeanId instance for the beanId String and then use the addBean(BeanId, Object, Fragment) method.

        Parameters:
        beanId - The beanId under which the bean is to be stored.
        bean - The bean instance to be stored.
        source - Source fragment.
      • getBeanId

        BeanId getBeanId​(String beanId)
        Get the BeanId instance for the specified beanId String.

        Regsiters the beanId if it's not already registered.

        Parameters:
        beanId - The beanId String.
        Returns:
        The associated BeanId instance.
      • containsBean

        boolean containsBean​(BeanId beanId)
        Looks if a bean instance is set under the BeanId
        Parameters:
        beanId - The BeanId under which is looked.
      • getBean

        Object getBean​(BeanId beanId)
        Get the current bean, specified by the supplied BeanId.

        Parameters:
        beanId - The BeanId of the bean to be returned.
        Returns:
        The bean instance, otherwise null.
      • getBean

        Object getBean​(String beanId)
        Returns the bean by it's beanId name.

        Returns the first bean of the specified type from the BeanContext instance.

        Parameters:
        beanId - The type of the bean to be returned.
        Returns:
        The bean instance, otherwise null.
      • getBean

        <T> T getBean​(Class<T> beanType)
        Returns the bean by it's beanId name.

        Returns the first bean of the specified type from the BeanContext instance.

        Parameters:
        beanType - The type of the bean to be returned.
        Returns:
        The bean instance, otherwise null.
      • changeBean

        void changeBean​(BeanId beanId,
                        Object bean,
                        Fragment<?> source)
        Changes a bean instance of the given BeanId. The difference to addBean(BeanId, Object) is that the bean must exist, the associated beans aren't removed and the observers of the BeanLifecycle.CHANGE event are notified.
        Parameters:
        beanId - The BeanId under which the bean instance is to be stored.
        bean - The bean instance to be stored.
        source - Source fragment.
      • removeBean

        Object removeBean​(BeanId beanId,
                          Fragment<?> source)
        Removes a bean and all its associated lifecycle beans from the bean map
        Parameters:
        beanId - The beanId to remove the beans from.
        source - Source fragment.
      • removeBean

        Object removeBean​(String beanId,
                          Fragment<?> source)
        Removes a bean and all its associated lifecycle beans from the bean map
        Parameters:
        beanId - The beanId to remove the beans from.
        source - Source fragment.
      • clear

        void clear()
        Removes all the beans from the bean map
      • addObserver

        void addObserver​(BeanContextLifecycleObserver observer)
        Registers a bean context observer.
        Parameters:
        observer - The actual BeanObserver instance.
      • notifyObservers

        void notifyObservers​(BeanContextLifecycleEvent event)
        Notify all observers of a specific bean lifecycle event.
        Parameters:
        event - The event.
      • removeObserver

        void removeObserver​(BeanContextLifecycleObserver observer)
        Unregisters a bean observer.
        Parameters:
        observer - The actual BeanObserver instance.
      • getBeanMap

        Map<String,​Object> getBeanMap()
        This returns a map which is backed by this repository. Changes made in the map are reflected back into the repository. There are some important side notes:
        • The write performance of the map isn't as good as the write performance of the BeanRepository because it needs to find or register the BeanId every time. The read performance are as good as any normal Map.
        • The entrySet() method returns an UnmodifiableSet
        • When a bean gets removed from the BeanRepository then only the value of the map entry is set to null. This means that null values should be regarded as deleted beans. That is also why the size() of the bean map isn't accurate. It also counts the null value entries.
        Only use the Map if you absolutely needed it else you should use the BeanRepository.
      • setBeanInContext

        void setBeanInContext​(BeanId beanId,
                              boolean inContext)
        Mark the bean as being in context.

        This is "set" when we enter the fragment around which the bean is created and unset when we exit.

        Parameters:
        beanId - The bean ID.
        inContext - True if the bean is in context, otherwise false.