Interface BeanContext

  • All Superinterfaces:
    AutoCloseable, Closeable

    public interface BeanContext
    extends Closeable
    Holds beans created by dependency injection.

    The beans have singleton scope, support lifecycle methods for postConstruct and preDestroy and are created (wired) via dependency injection.

    Create a BeanContext

    We can programmatically create a BeanContext via BeanContextBuilder.

    
    
       // create a BeanContext ...
    
       try (BeanContext context = new BeanContextBuilder()
         .build()) {
    
         CoffeeMaker coffeeMaker = context.getBean(CoffeeMaker.class);
         coffeeMaker.makeIt()
       }
    
     

    Implicitly used

    The BeanContext is implicitly used by SystemContext. It will be created as needed and a shutdown hook will close the underlying BeanContext on JVM shutdown.

    
    
       // BeanContext created as needed under the hood
    
       CoffeeMaker coffeeMaker = SystemContext.getBean(CoffeeMaker.class);
       coffeeMaker.brew();
    
     
    • Method Detail

      • getBean

        <T> T getBean​(Class<T> type)
        Return a single bean given the type.
        
        
           CoffeeMaker coffeeMaker = beanContext.getBean(CoffeeMaker.class);
           coffeeMaker.brew();
        
         
        Parameters:
        type - an interface or bean type
      • getBean

        <T> T getBean​(Class<T> type,
                      String name)
        Return a single bean given the type and name.
        
        
           Heater heater = beanContext.getBean(Heater.class, "electric");
           heater.heat();
        
         
        Parameters:
        type - an interface or bean type
        name - the name qualifier of a specific bean
      • getBeansWithAnnotation

        List<ObjectgetBeansWithAnnotation​(Class<?> annotation)
        Return the list of beans that have an annotation.
        
        
           // e.g. register all controllers with web a framework
           // .. where Controller is an annotation on the beans
        
           List<Object> controllers = SystemContext.getBeansWithAnnotation(Controller.class);
        
         

        The classic use case for this is registering controllers or routes to web frameworks like Sparkjava, Javlin, Rapidoid etc.

        Parameters:
        annotation - An annotation class.
      • getBeans

        <T> List<T> getBeans​(Class<T> interfaceType)
        Return the list of beans that implement the interface.
        
        
           // e.g. register all routes for a web framework
        
           List<WebRoute> routes = SystemContext.getBeans(WebRoute.class);
        
         
        Parameters:
        interfaceType - An interface class.
      • getBeansByPriority

        <T> List<T> getBeansByPriority​(Class<T> interfaceType)
        Return the list of beans that implement the interface sorting by priority (ignoring any Priority annotation).
      • sortByPriority

        <T> List<T> sortByPriority​(List<T> list)
        Sort the beans by javax.annotation.Priority annotation.
        Parameters:
        list - The beans to sort by priority
        Returns:
        A new list of beans sorted by priority
      • start

        void start()
        Start the context firing any @PostConstruct methods.