Interface BasicComponentRegistry
-
- All Known Implementing Classes:
BasicComponentRegistryImpl
public interface BasicComponentRegistry
Basic dependency injection container.Components are identified by a component name
String
, which is usually the fully-qualified name of a class or interface. Components may have aliases, e.g. the cache is visible both asorg.infinispan.Cache
and asorg.infinispan.AdvancedCache
.Components can either be registered explicitly with
registerComponent(String, Object, boolean)
, or constructed by a factory and registered implicitly when another component depends on them.During registration (either explicit or implicit), fields and and methods annotated with
Inject
are injected with other components. The name of the dependency is usually the fully-qualified name of the field or parameter type, but it can be overridden with theComponentName
annotation.A factory is a component implementing
ComponentFactory
and annotated withDefaultFactoryFor
. A factory must have the same scope as the components it creates. A factory implementingAutoInstantiableFactory
does not have to be registered explicitly, the registry will be create and register it on demand.Each registry has a scope, and
Scopes.NAMED_CACHE
-scoped registries delegate to aScopes.GLOBAL
registry.registerComponent(String, Object, boolean)
will throw a CacheConfigurationException if the component has aScope
annotation withe a different scope. Implicitly created dependencies are automatically registered in the right scope based on theirScope
annotation.Dependency cycles are not allowed. Declaring the dependency field of type
ComponentRef
, breaks the cycle by allowing the registry to inject/start the dependency lazily.For all the components that implement
Lifecycle
, the registry calls Lifecycle.start() during startup, in the order they were registered. If a component is registered when the registry is already running, Lifecycle.start() is called during registration.During shutdown, registration of new components is not allowed. The registry stops all the components in the reverse order of their start, by invoking all the methods annotated with
Stop
. The
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description void
addDynamicDependency(String ownerComponentName, String dependencyComponentName)
Add a dynamic dependency between two components.default <T> ComponentRef<T>
getComponent(Class<T> componentType)
Looks up a running component namedname
in the registry, or registers it if necessary.<T> ComponentRef<T>
getComponent(String name, Class<T> componentType)
Looks up a running component namedname
in the registry, or registers it if necessary.Collection<ComponentRef<?>>
getRegisteredComponents()
Runconsumer
for each registered component in the current scope.void
registerAlias(String aliasName, String targetComponentName, Class<?> targetComponentType)
Register an alias to another component.default <T> ComponentRef<T>
registerComponent(Class<?> componentType, T instance, boolean manageLifecycle)
Register a component namedcomponentType.getName()
.<T> ComponentRef<T>
registerComponent(String componentName, T instance, boolean manageLifecycle)
Register a component namedcomponentName
.void
replaceComponent(String componentName, Object newInstance, boolean manageLifecycle)
Replace an existing component.void
rewire()
Rewire all the injections after a component was replaced withreplaceComponent(String, Object, boolean)
.void
stop()
void
wireDependencies(Object target, boolean startDependencies)
Look up the dependencies oftarget
as if it were a component, and inject them.
-
-
-
Method Detail
-
getComponent
<T> ComponentRef<T> getComponent(String name, Class<T> componentType)
Looks up a running component namedname
in the registry, or registers it if necessary.If another thread is registering the component, wait for the other thread to finish.
The component is wired (dependencies are injected) during registration. Use
ComponentRef.running()
to start the component.- Parameters:
name
- The component name.componentType
- The expected component type, not used to identify the component.
-
getComponent
default <T> ComponentRef<T> getComponent(Class<T> componentType)
Looks up a running component namedname
in the registry, or registers it if necessary.
-
registerComponent
<T> ComponentRef<T> registerComponent(String componentName, T instance, boolean manageLifecycle)
Register a component namedcomponentName
.If the component has dependencies, look them up using
getComponent(String, Class)
and inject them.- Parameters:
componentName
- The component name.instance
- The component instance.manageLifecycle
-false
if the registry should ignore methods annotated with Start and Stop- Throws:
IllegalLifecycleStateException
- If the registry is stopping/stoppedCacheConfigurationException
- If a component/alias is already registered with that name, or if a dependency cannot be resolved
-
registerComponent
default <T> ComponentRef<T> registerComponent(Class<?> componentType, T instance, boolean manageLifecycle)
Register a component namedcomponentType.getName()
.
-
registerAlias
void registerAlias(String aliasName, String targetComponentName, Class<?> targetComponentType)
Register an alias to another component.Components that depend on the alias will behave as if they depended on the original component directly.
- Throws:
IllegalLifecycleStateException
- If the registry is stopping/stoppedCacheConfigurationException
- If a component/alias is already registered with that name
-
wireDependencies
void wireDependencies(Object target, boolean startDependencies)
Look up the dependencies oftarget
as if it were a component, and inject them.Behaves as if every dependency was resolved with
getComponent(String, Class)
.- Parameters:
target
- An instance of a class withInject
annotations.startDependencies
- Iftrue
, start the dependencies before injecting them.- Throws:
IllegalLifecycleStateException
- If the registry is stopping/stoppedCacheConfigurationException
- If a dependency cannot be resolved
-
addDynamicDependency
void addDynamicDependency(String ownerComponentName, String dependencyComponentName)
Add a dynamic dependency between two components.- Parameters:
ownerComponentName
- The dependent component's name.dependencyComponentName
- The component depended on.Note: doesn't have any effect if the owner component is already started. The stop order is determined exclusively by the start order.
-
replaceComponent
void replaceComponent(String componentName, Object newInstance, boolean manageLifecycle)
Replace an existing component. For testing purposes only, NOT THREAD-SAFE.Dependencies will be injected, and the start/stop methods will run if
manageLifecycle
istrue
. The new component is stopped exactly when the replaced component would have been stopped, IGNORING DEPENDENCY CHANGES. Need to callrewire()
to inject the new component in all the components that depend on it. If the component is global, need to callrewire()
on all the cache component registries as well.- Throws:
IllegalLifecycleStateException
- If the registry is stopping/stopped
-
rewire
void rewire()
Rewire all the injections after a component was replaced withreplaceComponent(String, Object, boolean)
. For testing purposes only.
-
getRegisteredComponents
Collection<ComponentRef<?>> getRegisteredComponents()
Runconsumer
for each registered component in the current scope.
-
stop
void stop()
-
-