public class DefaultBeanContext extends Object implements BeanContext
A default implementation of the BeanContext
interface.
This class allows full access to all beans defined in a hierarchy of
BeanStore
s. Dependencies are dynamically resolved and injected.
The class is thread-safe. When operating on a bean store hierarchy it
implements transactional behavior as described in the documentation to the
BeanStore
interface, i.e. if two threads try to access a
BeanProvider
concurrently, one of the will be suspended until the
other one has resolved all of its dependencies. This enables concurrent,
synchronized access to bean stores in a read-only manner. However the bean
stores should not be written at the same time.
Constructor and Description |
---|
DefaultBeanContext()
Creates a new instance of
DefaultBeanContext . |
DefaultBeanContext(BeanStore defStore)
Creates a new instance of
DefaultBeanContext and sets the default
bean store. |
Modifier and Type | Method and Description |
---|---|
void |
addBeanCreationListener(BeanCreationListener l)
Adds the specified
BeanCreationListener to this context. |
Set<Class<?>> |
beanClasses()
Obtains a list of the classes of the beans defined in the default bean
store.
|
Set<Class<?>> |
beanClasses(BeanStore store)
Obtains a list of the classes of the beans defined in the given store and
its parents.
|
String |
beanNameFor(BeanProvider beanProvider)
Searches for the specified
BeanProvider in the accessible bean
stores (starting with the default bean store) and the returns the name,
under which it is registered. |
String |
beanNameFor(BeanProvider beanProvider,
BeanStore store)
Searches for the specified
BeanProvider in the accessible bean
stores (starting with the specified bean store) and the returns the name,
under which it is registered. |
Set<String> |
beanNames()
Returns a set with the names of the beans defined in the default bean
store.
|
Set<String> |
beanNames(BeanStore store)
Returns a set with the names of the beans defined in the given bean store
(or its parent).
|
void |
close()
Closes this
BeanContext . |
boolean |
containsBean(Class<?> beanClass)
Tests whether a bean with the given bean class can be found in the
default bean store.
|
boolean |
containsBean(Class<?> beanClass,
BeanStore store)
Tests whether a bean with the given class can be found in the specified
bean store.
|
boolean |
containsBean(String name)
Tests whether a bean with the given name can be found in the default
store.
|
boolean |
containsBean(String name,
BeanStore store)
Tests whether a bean with the given name can be found in the specified
bean store.
|
<T> T |
getBean(Class<T> beanCls)
Returns the bean with the specified class from the default bean store.
|
<T> T |
getBean(Class<T> beanCls,
BeanStore store)
Returns the bean with the specified class from the given bean store.
|
protected Object |
getBean(Dependency dependency,
BeanStore store)
Obtains the bean from the
BeanProvider specified by the given
Dependency . |
Object |
getBean(String name)
Returns the bean with the specified name from the default store.
|
Object |
getBean(String name,
BeanStore store)
Returns the bean with the specified name from the given bean store.
|
ClassLoaderProvider |
getClassLoaderProvider()
Returns the
ClassLoaderProvider used by this bean context. |
BeanStore |
getDefaultBeanStore()
Returns the default bean store.
|
void |
removeBeanCreationListener(BeanCreationListener l)
Removes the specified
BeanCreationListener from this context. |
void |
setClassLoaderProvider(ClassLoaderProvider classLoaderProvider)
Sets the
ClassLoaderProvider to be used by this bean context. |
void |
setDefaultBeanStore(BeanStore store)
Sets the default bean store.
|
protected void |
waitForTx(BeanStore root)
Waits at the specified bean store until the current transaction finishes.
|
public DefaultBeanContext()
DefaultBeanContext
.public DefaultBeanContext(BeanStore defStore)
DefaultBeanContext
and sets the default
bean store. This parameter is optional. If no BeanStore
is
provided, a default store must be set later or only methods can be used
that expect a store as argument.defStore
- the default bean storepublic ClassLoaderProvider getClassLoaderProvider()
ClassLoaderProvider
used by this bean context.getClassLoaderProvider
in interface BeanContext
ClassLoaderProvider
setClassLoaderProvider(ClassLoaderProvider)
public void setClassLoaderProvider(ClassLoaderProvider classLoaderProvider)
ClassLoaderProvider
to be used by this bean context. The
ClassLoaderProvider
is needed when dependencies to beans are to
be resolved that specified by class names. When a new DefaultBeanContext
instance is created, a default ClassLoaderProvider
is set. It is then possible to change this object
using this method.setClassLoaderProvider
in interface BeanContext
classLoaderProvider
- the new ClassLoaderProvider
(must not
be null)IllegalArgumentException
- if the parameter is nullpublic Set<Class<?>> beanClasses()
beanClasses
in interface BeanContext
public Set<Class<?>> beanClasses(BeanStore store)
beanClasses
in interface BeanContext
store
- the store to start frompublic Set<String> beanNames()
beanNames
in interface BeanContext
public Set<String> beanNames(BeanStore store)
beanNames
in interface BeanContext
store
- the bean storepublic boolean containsBean(String name)
containsBean
in interface BeanContext
name
- the name of the beanpublic boolean containsBean(String name, BeanStore store)
containsBean
in interface BeanContext
name
- the name of the beanstore
- the bean storepublic boolean containsBean(Class<?> beanClass)
containsBean
in interface BeanContext
beanClass
- the class of the beanpublic boolean containsBean(Class<?> beanClass, BeanStore store)
containsBean
in interface BeanContext
beanClass
- the class of the beanstore
- the bean storepublic Object getBean(String name)
getBean
in interface BeanContext
name
- the name of the bean to retrieveInjectionException
- if an error occurspublic Object getBean(String name, BeanStore store)
getBean
in interface BeanContext
name
- the name of the bean to retrievestore
- the bean storeInjectionException
- if an error occurspublic <T> T getBean(Class<T> beanCls)
getBean
in interface BeanContext
T
- the type of the bean to be retrievedbeanCls
- the class of the bean to be retrievedInjectionException
- if an error occurspublic <T> T getBean(Class<T> beanCls, BeanStore store)
getBean
in interface BeanContext
T
- the type of the bean to be retrievedbeanCls
- the class of the bean to be retrievedstore
- the bean storeInjectionException
- if an error occurspublic BeanStore getDefaultBeanStore()
getDefaultBeanStore
in interface BeanContext
public String beanNameFor(BeanProvider beanProvider)
BeanProvider
in the accessible bean
stores (starting with the default bean store) and the returns the name,
under which it is registered.beanNameFor
in interface BeanContext
beanProvider
- the BeanProvider
in questionBeanContext.beanNameFor(BeanProvider, BeanStore)
public String beanNameFor(BeanProvider beanProvider, BeanStore store)
BeanProvider
in the accessible bean
stores (starting with the specified bean store) and the returns the name,
under which it is registered.beanNameFor
in interface BeanContext
beanProvider
- the BeanProvider
in questionstore
- the BeanStore
public void close()
BeanContext
. This is just an empty dummy
implementation. There are no resources to be freed.close
in interface BeanContext
public void setDefaultBeanStore(BeanStore store)
setDefaultBeanStore
in interface BeanContext
store
- the new default bean storepublic void addBeanCreationListener(BeanCreationListener l)
BeanCreationListener
to this context.addBeanCreationListener
in interface BeanContext
l
- the listener to add (must not be null)IllegalArgumentException
- if the listener is nullpublic void removeBeanCreationListener(BeanCreationListener l)
BeanCreationListener
from this context.removeBeanCreationListener
in interface BeanContext
l
- the listener to be removedprotected Object getBean(Dependency dependency, BeanStore store)
BeanProvider
specified by the given
Dependency
. This method is called by the other
getBean()
methods. It does the real work.dependency
- the dependency to be resolvedstore
- the current storeInjectionException
- if an error occurs while resolving the
dependencyprotected void waitForTx(BeanStore root) throws InterruptedException
wait()
on the given bean store.root
- the root bean storeInterruptedException
- if the thread is interruptedCopyright © 2016 The JGUIraffe Team. All rights reserved.