Class SystemContext


  • public class SystemContext
    extends Object
    Provides a global system wide BeanContext that contains all the bean contexts in the classpath.

    This will automatically get all the bean contexts and wire them all as necessary. It will use a shutdown hook to fire any @PreDestroy methods on beans.

    Example: get a bean

    
    
       CoffeeMaker coffeeMaker = SystemContext.getBean(CoffeeMaker.class);
       coffeeMaker.brew();
    
     

    Example: get all the beans implementing an interface

    
    
       // e.g. register all WebRoutes for a web framework
    
       List<WebRoute> routes = SystemContext.getBeans(WebRoute.class);
    
       // register all the routes ...
    
     

    Example: get all the 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);
    
       // register all the controllers ...
    
     
    • Method Detail

      • getBean

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

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

        public static 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);
        
         
        Parameters:
        annotation - An annotation class.
      • getBeans

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

        public static <T> List<T> getBeansByPriority​(Class<T> interfaceType)
        Return the list of beans that implement the interface ordering based on @Priority.
        
        
           // e.g. register all web routes with web a framework
        
           List<WebRoute> routes = SystemContext.getBeansByPriority(WebRoute.class);
        
         
        Parameters:
        interfaceType - An interface class.