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

      • newBuilder

        static BeanContextBuilder newBuilder()
        Build a bean context with options for shutdown hook and supplying test doubles.

        We would choose to use BeanContextBuilder in test code (for component testing) as it gives us the ability to inject test doubles, mocks, spy's etc.

        
        
           @Test
           public void someComponentTest() {
        
             MyRedisApi mockRedis = mock(MyRedisApi.class);
             MyDbApi mockDatabase = mock(MyDbApi.class);
        
             try (BeanContext context = BeanContext.newBuilder()
               .withBeans(mockRedis, mockDatabase)
               .build()) {
        
               // built with test doubles injected ...
               CoffeeMaker coffeeMaker = context.getBean(CoffeeMaker.class);
               coffeeMaker.makeIt();
        
               assertThat(...
             }
           }
        
         
      • 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.
      • getBeansByPriority

        <T> List<T> getBeansByPriority​(Class<T> interfaceType,
                                       Class<? extends Annotation> priority)
        Return the beans that implement the interface sorting by the priority annotation used.

        The priority annotation will typically be either javax.annotation.Priority or jakarta.annotation.Priority.

        Parameters:
        interfaceType - The interface type of the beans to return
        priority - The priority annotation used to sort the beans
      • 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
      • sortByPriority

        <T> List<T> sortByPriority​(List<T> list,
                                   Class<? extends Annotation> priority)
        Sort the beans using the given Priority annotation.

        The priority annotation will typically be either javax.annotation.Priority or jakarta.annotation.Priority.

        Parameters:
        list - The beans to sort by priority
        priority - The priority annotation used to sort the beans
        Returns:
        A new list of beans sorted by priority
      • start

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