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 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.

    Since:
    9.4
    Author:
    Dan Berindei
    • Method Detail

      • getComponent

        <T,​U extends T> ComponentRef<T> getComponent​(String name,
                                                           Class<U> componentType)
        Looks up a running component named 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.

        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 named name in the registry, or registers it if necessary.
      • registerComponent

        <T> ComponentRef<T> registerComponent​(String componentName,
                                              T instance,
                                              boolean manageLifecycle)
        Register a component named componentName.

        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/stopped
        CacheConfigurationException - 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 named componentType.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/stopped
        CacheConfigurationException - If a component/alias is already registered with that name
      • wireDependencies

        void wireDependencies​(Object target,
                              boolean startDependencies)
        Look up the dependencies of target 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 with Inject annotations.
        startDependencies - If true, start the dependencies before injecting them.
        Throws:
        IllegalLifecycleStateException - If the registry is stopping/stopped
        CacheConfigurationException - 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

        @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 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.

        Throws:
        IllegalLifecycleStateException - If the registry is stopping/stopped
      • hasComponentAccessor

        @Experimental
        boolean hasComponentAccessor​(String componentClassName)
        Check if a component accessor has been registered for class componentClassName
      • stop

        void stop()
        Stop the registry and all the components that have been started.

        Components cannot be instantiated or started after this.

      • lazyGetComponent

        @Experimental
        <T> ComponentRef<T> lazyGetComponent​(Class<T> componentType)
        Looks up a component named name in the registry, or registers it if necessary. The component isn't instantiated neither running. Invoke ComponentRef.running() to instantiate and start it.
        Parameters:
        componentType - The expected component type, not used to identify the component.