Package org.silverpeas.kernel
Interface BeanContainer
public interface BeanContainer
The bean container is a wrapper of an IoC (Inversion of Control) by IoD (Inversion of Dependency)
system. Only one IoC system should be available and used in the whole Silverpeas runtime, meaning
only one implementation of this interface will be used. The expectations here is first the IoC
subsystem should implement the JSR-330 and as such it should support IoD; examples of such
subsystems are Spring, Micronaut or CDI (JSR-365). Second, the IoC subsystem must provide a way
to get a managed bean for non-managed beans. Moreover, the bean container imposes an
additional constrain to ease the use of IoD in Silverpeas whatever the underlying implementation:
the dependencies shouldn't be resolved through constructor or method parameters but instead by
using the
Inject annotation on fields or by using a
ManagedBeanProvider instance. Keep in mind to leave your code agnostic from any specific
code of an IoC solution so it should be more easy later to change such a solution.
The goal of the bean container is to manage the life-cycle of some objects (named here beans), to resolve and to satisfy automatically and in a transparent way their dependencies. For doing, the classes for which the instances have to be managed by the container should be elective for life-cycle management and registered into this container. The way the election and the registration of theses classes are performed is the responsibility of the underlying IoC subsystem.
- Author:
- mmoquillon
- API Note:
- The
BeanContainerisn't to be used directly by codes. It is just a wrapper of a true IoC by IoD solution for theManagedBeanProvider. Latter has to be used instead. - Implementation Requirements:
- The
BeanContainerhas to be bound to its implementation by the Java SPI (Java Service Provider Interface)
-
Method Summary
Modifier and TypeMethodDescription<T> Set<T> getAllBeansByType(Class<T> type, Annotation... qualifiers) Gets a bean managed in this container by its name.<T> Optional<T> getBeanByName(String name) Gets a bean managed in this container by its name.<T> Optional<T> getBeanByType(Class<T> type, Annotation... qualifiers) Gets a bean managed in this container by its type and optionally by some qualifiers.
-
Method Details
-
getBeanByName
Gets a bean managed in this container by its name. If no such bean exists in the bean container, nothing is returned.- Type Parameters:
T- the type of the bean to return.- Parameters:
name- the name of the bean.- Returns:
- the bean matching the specified name or nothing
- Throws:
MultipleCandidateException- if there is more than one bean with the given name as the name must be unique for each bean.ExpectationViolationException- if the inner expectations of the container on the bean to get aren't satisfied.IllegalStateException- if the container isn't in the state of answering.
-
getBeanByType
Gets a bean managed in this container by its type and optionally by some qualifiers.- Type Parameters:
T- the type of the bean to return.- Parameters:
type- the type of the bean.qualifiers- zero, one or more qualifiers annotating the bean to look for.- Returns:
- the bean satisfying the expected type and, if any, the expected qualifiers, or nothing.
- Throws:
MultipleCandidateException- if there is more than one bean matching the given type and qualifiers as there is an ambiguous decision in selecting the bean to return.ExpectationViolationException- if the inner expectations of the container on the bean to get or on the qualifiers aren't satisfied.IllegalStateException- if the container isn't in the state of answering.
-
getAllBeansByType
Gets a bean managed in this container by its name. If no such bean exists in the bean container, then an empty set is returned.- Type Parameters:
T- the type of the bean to return.- Parameters:
type- the type of the bean.qualifiers- zero, one or more qualifiers annotating the bean to look for.- Returns:
- a set of beans satisfying the expected type and, if any, the expected qualifiers, or an empty set otherwise.
- Throws:
ExpectationViolationException- if the inner expectations of the container on the beans to get or on the qualifiers aren't satisfied.IllegalStateException- if the container isn't in the state of answering.
-