public interface BasicComponentRegistry
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
and
PostStart
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
.
Modifier and Type | Method and 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 named
name in the registry, or registers it if necessary. |
<T,U extends T> |
getComponent(String name,
Class<U> componentType)
Looks up a running component named
name in the registry, or registers it if necessary. |
MBeanMetadata |
getMBeanMetadata(String className) |
Collection<ComponentRef<?>> |
getRegisteredComponents()
Run
consumer for each registered component in the current scope. |
boolean |
hasComponentAccessor(String componentClassName)
Check if a component accessor has been registered for class
componentClassName |
<T> ComponentRef<T> |
lazyGetComponent(Class<T> componentType)
Looks up a component named
name 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 named
componentType.getName() . |
<T> ComponentRef<T> |
registerComponent(String componentName,
T instance,
boolean manageLifecycle)
Register a component named
componentName . |
void |
replaceComponent(String componentName,
Object newInstance,
boolean manageLifecycle)
Replace an existing component.
|
void |
rewire()
Rewire all the injections after a component was replaced with
replaceComponent(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 of
target as if it were a component, and inject them. |
<T,U extends T> ComponentRef<T> getComponent(String name, Class<U> componentType)
name
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.
name
- The component name.componentType
- The expected component type, not used to identify the component.default <T> ComponentRef<T> getComponent(Class<T> componentType)
name
in the registry, or registers it if necessary.<T> ComponentRef<T> registerComponent(String componentName, T instance, boolean manageLifecycle)
componentName
.
If the component has dependencies, look them up using getComponent(String, Class)
and inject them.
componentName
- The component name.instance
- The component instance.manageLifecycle
- false
if the registry should ignore methods annotated with
Start and StopIllegalLifecycleStateException
- If the registry is stopping/stoppedCacheConfigurationException
- If a component/alias is already registered with that
name, or if a dependency cannot be resolveddefault <T> ComponentRef<T> registerComponent(Class<?> componentType, T instance, boolean manageLifecycle)
componentType.getName()
.void registerAlias(String aliasName, String targetComponentName, Class<?> targetComponentType)
Components that depend on the alias will behave as if they depended on the original component directly.
IllegalLifecycleStateException
- If the registry is stopping/stoppedCacheConfigurationException
- If a component/alias is already registered with that
namevoid wireDependencies(Object target, boolean startDependencies)
target
as if it were a component, and inject them.
Behaves as if every dependency was resolved with getComponent(String, Class)
.
target
- An instance of a class with Inject
annotations.startDependencies
- If true
, start the dependencies before injecting them.IllegalLifecycleStateException
- If the registry is stopping/stoppedCacheConfigurationException
- If a dependency cannot be resolvedvoid addDynamicDependency(String ownerComponentName, String dependencyComponentName)
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.
@Experimental void replaceComponent(String componentName, Object newInstance, boolean manageLifecycle)
Dependencies will be injected, and the start/stop methods will run if manageLifecycle
is true
.
The new component is stopped exactly when the replaced component would have been stopped,
IGNORING DEPENDENCY CHANGES.
Need to call rewire()
to inject the new component in all the components that depend on it.
If the component is global, need to call rewire()
on all the cache component registries as well.
IllegalLifecycleStateException
- If the registry is stopping/stopped@Experimental void rewire()
replaceComponent(String, Object, boolean)
.
For testing purposes only.@Experimental Collection<ComponentRef<?>> getRegisteredComponents()
consumer
for each registered component in the current scope.@Experimental MBeanMetadata getMBeanMetadata(String className)
className
@Experimental boolean hasComponentAccessor(String componentClassName)
componentClassName
void stop()
Components cannot be instantiated or started after this.
@Experimental <T> ComponentRef<T> lazyGetComponent(Class<T> componentType)
name
in the registry, or registers it if necessary.
The component isn't instantiated neither running. Invoke ComponentRef.running()
to instantiate and start it.componentType
- The expected component type, not used to identify the component.Copyright © 2021 JBoss by Red Hat. All rights reserved.