Interface BasicComponentRegistry
- All Known Implementing Classes:
BasicComponentRegistryImpl
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 as org.infinispan.Cache
and as
org.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 the ComponentName
annotation.
A factory is a component implementing ComponentFactory
and annotated with
DefaultFactoryFor
.
A factory must have the same scope as the components it creates.
A factory implementing AutoInstantiableFactory
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 a Scopes.GLOBAL
registry.
registerComponent(String, Object, boolean)
will throw a
CacheConfigurationException if the component has a
Scope
annotation withe a different scope.
Implicitly created dependencies are automatically registered in the right scope based on their
Scope
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.
Components are not started by default.
Methods annotated with Start
are invoked only on
ComponentRef.running()
.
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
.
- Since:
- 9.4
- Author:
- Dan Berindei
-
Method Summary
Modifier and TypeMethodDescriptionvoid
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,
U extends T>
ComponentRef<T>getComponent
(String name, Class<U> componentType) Looks up a running component namedname
in the registry, or registers it if necessary.getMBeanMetadata
(String className) Runconsumer
for each registered component in the current scope.boolean
hasComponentAccessor
(String componentClassName) Check if a component accessor has been registered for classcomponentClassName
<T> ComponentRef<T>
lazyGetComponent
(Class<T> componentType) Looks up a component namedname
in the registry, or registers it if necessary.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()
Stop the registry and all the components that have been started.void
wireDependencies
(Object target, boolean startDependencies) Look up the dependencies oftarget
as if it were a component, and inject them.
-
Method Details
-
getComponent
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
Looks up a running component namedname
in the registry, or registers it if necessary. -
registerComponent
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
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
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
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
@Experimental 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
Rewire all the injections after a component was replaced withreplaceComponent(String, Object, boolean)
. For testing purposes only. -
getRegisteredComponents
Runconsumer
for each registered component in the current scope. -
getMBeanMetadata
- Returns:
- The MBean metadata for class
className
-
hasComponentAccessor
Check if a component accessor has been registered for classcomponentClassName
-
stop
void stop()Stop the registry and all the components that have been started.Components cannot be instantiated or started after this.
-
lazyGetComponent
Looks up a component namedname
in the registry, or registers it if necessary. The component isn't instantiated neither running. InvokeComponentRef.running()
to instantiate and start it.- Parameters:
componentType
- The expected component type, not used to identify the component.
-