Package net.sf.jguiraffe.di
Interface BeanContext
-
- All Known Implementing Classes:
BeanContextWrapper,DefaultBeanContext
public interface BeanContextDefinition of an interface that allows access to the beans managed by the dependency injection framework.
This interface serves as the top-level entry point into the dependency injection framework. It allows querying beans from a specified
or a defaultBeanStoreBeanStoreby name or by type. Listing the available beans is also possible.When objects are dynamically created, class loader issues have to be taken into account. A
BeanContextallows the registration of class loaders by name. In the configuration, it can then be specified, which bean is to be created using which class loader. For the context class loader there is a special constant.- Version:
- $Id: BeanContext.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description voidaddBeanCreationListener(BeanCreationListener l)Adds a newBeanCreationListenerto this context.Set<Class<?>>beanClasses()Returns a set with the classes of all beans defined in the defaultBeanStore(or one of its parents).Set<Class<?>>beanClasses(BeanStore store)Returns a set with the classes of all beans defined in the givenBeanStore.StringbeanNameFor(BeanProvider beanProvider)Returns the name of the bean that is managed by the specifiedBeanProvider, starting search with the defaultBeanStore.StringbeanNameFor(BeanProvider beanProvider, BeanStore store)Returns the name of the bean that is managed by the specifiedBeanProvider, starting search in the specifiedBeanStore.Set<String>beanNames()Returns a set with the names of all beans defined in the defaultBeanStore.Set<String>beanNames(BeanStore store)Returns a set with the names of all beans defined in the givenBeanStore.voidclose()Closes this bean context.booleancontainsBean(Class<?> beanClass)Checks whether the defaultBeanStorecontains a bean with the given class.booleancontainsBean(Class<?> beanClass, BeanStore store)Checks whether the specifiedBeanStorecontains a bean with the given class.booleancontainsBean(String name)Checks whether the defaultBeanStorecontains a bean with the given name.booleancontainsBean(String name, BeanStore store)Checks whether the specifiedBeanStorecontains a bean with the given name.<T> TgetBean(Class<T> beanCls)Returns the bean with the specified class from the defaultBeanStore.<T> TgetBean(Class<T> beanCls, BeanStore store)Returns the bean with the specified class from the givenBeanStore.ObjectgetBean(String name)Returns the bean with the specified name from the defaultBeanStore.ObjectgetBean(String name, BeanStore store)Returns the bean with the specified name from the givenBeanStore.ClassLoaderProvidergetClassLoaderProvider()Returns theClassLoaderProviderused by this context.BeanStoregetDefaultBeanStore()Returns the default bean store.voidremoveBeanCreationListener(BeanCreationListener l)Removes the specifiedBeanCreationListenerfrom this context.voidsetClassLoaderProvider(ClassLoaderProvider clp)Sets theClassLoaderProviderto be used by this context.voidsetDefaultBeanStore(BeanStore store)Sets the default bean store.
-
-
-
Method Detail
-
getBean
Object getBean(String name)
Returns the bean with the specified name from the defaultBeanStore.- Parameters:
name- the name of the bean to be retrieved- Returns:
- the bean with this name
- Throws:
InjectionException- if the bean cannot be resolved or its creation or initialization causes an error
-
getBean
Object getBean(String name, BeanStore store)
Returns the bean with the specified name from the givenBeanStore. This method can be used if a child bean store of the default bean store is to be accessed.- Parameters:
name- the name of the bean to be retrievedstore- theBeanStoreto query- Returns:
- the bean with this name
- Throws:
InjectionException- if the bean cannot be resolved or its creation or initialization causes an errorIllegalArgumentException- if the passed in bean store is null
-
getBean
<T> T getBean(Class<T> beanCls)
Returns the bean with the specified class from the defaultBeanStore. An implementation of this method has to iterate over the beans defined in the bean store to find out, whether the specified class is assignable from their class. Then the first fitting bean will be returned. So it is for instance possible to query for an interface and find a bean that is a concrete implementation of this interface. If there are multiple bean definitions that are compatible with this class, it is up to a concrete implementation of this interface to choose one. Defining beans based on a class (or interface) allows to abstract from the concrete naming of beans and focuses more on the beans' semantics.- Type Parameters:
T- the type of the bean to be fetched- Parameters:
beanCls- the class of the bean to be fetched- Returns:
- the bean with this class
- Throws:
InjectionException- if the bean cannot be resolved or its creation or initialization causes an error
-
getBean
<T> T getBean(Class<T> beanCls, BeanStore store)
Returns the bean with the specified class from the givenBeanStore. Works like the overloaded method, but operates on the given bean store.- Type Parameters:
T- the type of the bean to be fetched- Parameters:
beanCls- the class of the bean to be fetchedstore- theBeanStoreto use- Returns:
- the bean with this class
- Throws:
InjectionException- if the bean cannot be resolved or its creation or initialization causes an errorIllegalArgumentException- if the passed in bean store is null
-
beanNames
Set<String> beanNames()
Returns a set with the names of all beans defined in the defaultBeanStore.- Returns:
- a set with the names of the defined beans
-
beanNames
Set<String> beanNames(BeanStore store)
Returns a set with the names of all beans defined in the givenBeanStore. This list will also contain beans that are defined in one of the bean store's parents.- Parameters:
store- the bean store (must not be null)- Returns:
- a set with the names of the defined beans
- Throws:
IllegalArgumentException- if the passed in bean store is null
-
beanClasses
Set<Class<?>> beanClasses()
Returns a set with the classes of all beans defined in the defaultBeanStore(or one of its parents).- Returns:
- a set with the classes of the defined beans
-
beanClasses
Set<Class<?>> beanClasses(BeanStore store)
Returns a set with the classes of all beans defined in the givenBeanStore. This list will also contain beans that are defined in one of the bean store's parents.- Parameters:
store- the bean store (must not be null)- Returns:
- a set with the classes of the defined beans
- Throws:
IllegalArgumentException- if the passed in bean store is null
-
containsBean
boolean containsBean(String name)
Checks whether the defaultBeanStorecontains a bean with the given name. If necessary the bean store's parents will also be searched.- Parameters:
name- the name of the searched bean- Returns:
- a flag whether this bean can be found
-
containsBean
boolean containsBean(String name, BeanStore store)
Checks whether the specifiedBeanStorecontains a bean with the given name. If necessary the bean store's parents will also be searched.- Parameters:
name- the name of the searched beanstore- the bean store- Returns:
- a flag whether this bean can be found
-
containsBean
boolean containsBean(Class<?> beanClass)
Checks whether the defaultBeanStorecontains a bean with the given class. If necessary the bean store's parents will also be searched.- Parameters:
beanClass- the class of the searched bean- Returns:
- a flag whether this bean can be found
-
containsBean
boolean containsBean(Class<?> beanClass, BeanStore store)
Checks whether the specifiedBeanStorecontains a bean with the given class. If necessary the bean store's parents will also be searched.- Parameters:
beanClass- the class of the searched beanstore- the bean store- Returns:
- a flag whether this bean can be found
-
getDefaultBeanStore
BeanStore getDefaultBeanStore()
Returns the default bean store.- Returns:
- the default bean store
-
setDefaultBeanStore
void setDefaultBeanStore(BeanStore store)
Sets the default bean store. This bean store is used as starting point for all lookup-operations if no specific bean store is specified.- Parameters:
store- the new default bean store
-
beanNameFor
String beanNameFor(BeanProvider beanProvider)
Returns the name of the bean that is managed by the specifiedBeanProvider, starting search with the defaultBeanStore.- Parameters:
beanProvider- theBeanProvider- Returns:
- the name, under which this
BeanProvideris registered in one of the accessible bean stores or null if it cannot be resolved - See Also:
beanNameFor(BeanProvider, BeanStore)
-
beanNameFor
String beanNameFor(BeanProvider beanProvider, BeanStore store)
Returns the name of the bean that is managed by the specifiedBeanProvider, starting search in the specifiedBeanStore. This method can be used for performing a "reverse lookup": when a bean provider is known, but the corresponding bean name is searched. A use case would be the processing of aBeanCreationEvent. The event object contains theBeanProviderthat created the bean, but the name of this bean is not part of the event.- Parameters:
beanProvider- theBeanProviderstore- theBeanStore- Returns:
- the name, under which this
BeanProvideris registered in one of the accessible bean stores or null if it cannot be resolved
-
addBeanCreationListener
void addBeanCreationListener(BeanCreationListener l)
Adds a newBeanCreationListenerto this context. This listener will receive notifications about newly created beans.- Parameters:
l- the listener to be added (must not be null)- Throws:
IllegalArgumentException- if the listener is null
-
removeBeanCreationListener
void removeBeanCreationListener(BeanCreationListener l)
Removes the specifiedBeanCreationListenerfrom this context. If the listener is not registered, this method has no effect.- Parameters:
l- the listener to remove
-
close
void close()
Closes this bean context. This method should be called when the context is no more needed. An implementation can free resources or perform other clean up. Note that theBeanStoreassociated with this context is not closed by this method (this is because aBeanStorecould be shared by multiple contexts, and there is not necessary a 1:1 relationship between aBeanStoreand aBeanContext).
-
getClassLoaderProvider
ClassLoaderProvider getClassLoaderProvider()
Returns theClassLoaderProviderused by this context.- Returns:
- the
ClassLoaderProvider
-
setClassLoaderProvider
void setClassLoaderProvider(ClassLoaderProvider clp)
Sets theClassLoaderProviderto be used by this context. This object is needed when classes have to be loaded through reflection.- Parameters:
clp- theClassLoaderProvider
-
-