Class ApplicationScope

java.lang.Object
io.avaje.inject.ApplicationScope

public class ApplicationScope
extends Object
Provides a global system wide BeanScope that contains all the beans.

This will automatically get all the beans 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 = ApplicationScope.get(CoffeeMaker.class);
   coffeeMaker.brew();

 

Example: get all the beans implementing an interface



   // e.g. register all WebRoutes for a web framework

   List<WebRoute> routes = ApplicationScope.list(WebRoute.class);

   // register all the routes ...

 
  • Method Details

    • scope

      public static BeanScope scope()
      Return the underlying BeanScope.
    • get

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

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

      public static <T> List<T> list​(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 = ApplicationScope.list(WebRoute.class);
      
       
      Parameters:
      interfaceType - An interface class.
    • listByPriority

      public static <T> List<T> listByPriority​(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 = ApplicationScope.listByPriority(WebRoute.class);
      
       
      Parameters:
      interfaceType - An interface class.
    • listByAnnotation

      public static List<Object> listByAnnotation​(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 = ApplicationScope.listByAnnotation(Controller.class);
      
       

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

      Parameters:
      annotation - An annotation class.
    • newRequestScope

      Start building a RequestScope.
      
      
         try (RequestScope requestScope = ApplicationScope.newRequestScope()
             // supply some instances
             .withBean(HttpRequest.class, request)
             .withBean(HttpResponse.class, response)
             .build()) {
      
             MyController controller = requestScope.get(MyController.class);
             controller.process();
      
         }
      
         ...
      
         // define request scoped beans
         @Request
         MyController {
      
           // can depend on supplied instances, singletons and other request scope beans
           @Inject
           MyController(HttpRequest request, HttpResponse response, MyService myService) {
             ...
           }
      
         }
      
       
      Returns:
      The request scope builder