Interface BeanResolver


  • public interface BeanResolver
    The main entry point for components looking to resolve a bean reference into a (usually user-provided) bean.

    Depending on the integration, beans may be instantiated using reflection (expecting a no-argument constructor), or provided by a more advanced dependency injection context (CDI, Spring DI).

    Regardless of the underlying implementation, this interface is used to resolve beans, referenced either resolve(Class, BeanRetrieval) by their type}, or by their type and name.

    It also offers ways to get references to configured beans of a given type.

    This interface is API, but should only be implemented by Hibernate Search itself; if you are looking to provide beans from a different source, you should implement BeanProvider instead.

    • Method Detail

      • resolve

        <T> BeanHolder<T> resolve​(Class<T> typeReference,
                                  BeanRetrieval retrieval)
        Resolve a bean by its type.
        Type Parameters:
        T - The expected return type.
        Parameters:
        typeReference - The type used as a reference to the bean to resolve. Must be non-null.
        retrieval - How to retrieve the bean. See BeanRetrieval.
        Returns:
        A BeanHolder containing the resolved bean.
        Throws:
        SearchException - if the reference is invalid (null) or the bean cannot be resolved.
      • resolve

        <T> BeanHolder<T> resolve​(Class<T> typeReference,
                                  String nameReference,
                                  BeanRetrieval retrieval)
        Resolve a bean by its name.
        Type Parameters:
        T - The expected return type.
        Parameters:
        typeReference - The type used as a reference to the bean to resolve. Must be non-null.
        nameReference - The name used as a reference to the bean to resolve. Must be non-null and non-empty.
        retrieval - How to retrieve the bean. See BeanRetrieval.
        Returns:
        A BeanHolder containing the resolved bean.
        Throws:
        SearchException - if a reference is invalid (null or empty) or the bean cannot be resolved.
      • resolve

        default <T> BeanHolder<T> resolve​(BeanReference<T> reference)
        Resolve a BeanReference.

        This method is just syntactic sugar to allow writing bridgeProvider::resolve and getting a Function<BeanReference<T>, T> that can be used in Optional.map(Function) for instance.

        Type Parameters:
        T - The expected return type.
        Parameters:
        reference - The reference to the bean to resolve. Must be non-null.
        Returns:
        A BeanHolder containing the resolved bean.
        Throws:
        SearchException - if the reference is invalid (null or empty) or the bean cannot be resolved.
      • resolve

        default <T> BeanHolder<List<T>> resolve​(List<? extends BeanReference<? extends T>> references)
        Resolve a list of BeanReferences.

        The main advantage of calling this method over looping and calling resolve(BeanReference) repeatedly is that errors are handled correctly: if a bean was already instantiated, and getting the next one fails, then the first bean will be properly closed before the exception is propagated. Also, this method returns a BeanHolder<List<T>> instead of a List<BeanHolder<T>>, so its result is easier to use in a try-with-resources.

        This method is also syntactic sugar to allow writing bridgeProvider::resolve and getting a Function<BeanReference<T>, T> that can be used in Optional.map(Function) for instance.

        Type Parameters:
        T - The expected bean type.
        Parameters:
        references - The references to the beans to retrieve. Must be non-null.
        Returns:
        A BeanHolder containing a List containing the resolved beans, in the same order as the references.
        Throws:
        SearchException - if one reference is invalid (null or empty) or the corresponding bean cannot be resolved.
      • allConfiguredForRole

        <T> List<BeanReference<T>> allConfiguredForRole​(Class<T> role)
        Return all the bean references configured for the given role.

        WARNING: this does not just return references to all the beans that implement role. Only beans registered during bean configuration are taken into account.

        Type Parameters:
        T - The expected bean type.
        Parameters:
        role - The role that must have been assigned to the retrieved beans. Must be non-null and non-empty.
        Returns:
        A List of bean references, possibly empty.
      • namedConfiguredForRole

        <T> Map<String,​BeanReference<T>> namedConfiguredForRole​(Class<T> role)
        Return named bean references configured for the given role.

        WARNING: this does not just return references to all the beans that implement role. Only beans registered during bean configuration are taken into account.

        Type Parameters:
        T - The expected bean type.
        Parameters:
        role - The role that must have been assigned to the retrieved beans. Must be non-null and non-empty.
        Returns:
        A Map from name to bean reference, possibly empty.