Interface Module.WrapperBeanBuilder<P,T,W extends Supplier<T>>

Type Parameters:
P - the type provided by the bean to build
T - the actual type of the bean to build
W - the bean wrapper which supplies the bean instance
All Superinterfaces:
Module.BeanBuilder<W,Module.WrapperBeanBuilder<P,T,W>>
Enclosing class:
Module

protected static interface Module.WrapperBeanBuilder<P,T,W extends Supplier<T>> extends Module.BeanBuilder<W,Module.WrapperBeanBuilder<P,T,W>>

A BeanBuilder for creating wrapper Module.Bean instances.

A wrapper Module.Bean instance is built from a Supplier used to obtain a wrapper instance which wraps the actual bean instance creation, initialization and destruction logic and defer the actual instantiation of the bean. Initialization and destruction operations are invoked on the wrapper instance (after dependency injection) and before its destruction respectively.

Particular care must be taken when a prototype wrapper bean is created as the wrapper bean instance must not hold any strong reference to the actual bean instance in order to prevent memory leaks. In that case, using a WeakReference is strongly advised.


 this.bean = WrapperBeanBuilder
     .singleton("bean", () -> {
          BeanA beanA = new BeanA(serviceSocket.get());
          return beanA;
      })
      .init(BeanA::init)
      .destroy(BeanA::destroy)
      .build(this);
 
Since:
1.0
Author:
Jeremy Kuhn
See Also:
  • Method Details

    • singleton

      static <T, W extends Supplier<T>> Module.WrapperBeanBuilder<T,T,W> singleton(String beanName, Supplier<W> constructor)

      Returns a singleton wrapper bean builder.

      Singleton Module.Beans are useful when one single instance of a bean should be injected through the application.

      Type Parameters:
      T - the type of the bean to build
      W - the bean wrapper which supplies the bean instance
      Parameters:
      beanName - the bean name
      constructor - the bean instance supplier
      Returns:
      a singleton Bean Builder
    • prototype

      static <T, W extends Supplier<T>> Module.WrapperBeanBuilder<T,T,W> prototype(String beanName, Supplier<W> constructor)

      Returns a prototype wrapper bean builder.

      Prototype Module.Beans are useful when distinct instances of a bean should be injected though the application.

      Type Parameters:
      T - the type of the bean to build
      W - the bean wrapper which supplies the bean instance
      Parameters:
      beanName - the bean name
      constructor - the bean instance supplier
      Returns:
      a prototype Bean Builder
    • build

      Module.Bean<P> build()

      Builds the bean.

      Returns:
      a bean
    • override

      <P> Module.WrapperBeanBuilder<P,T,W> override(Optional<Supplier<P>> override)

      Specifies an override that, when present, provides bean instances instead of the builder.

      Type Parameters:
      P - the type provided by the override and therefore the bean to build
      Parameters:
      override - An optional override
      Returns:
      this builder