The following is a simple example:

    1. int getCylinders()
    2. String start()
    3. }
    4. @Singleton (2)
    5. class V8Engine implements Engine {
    6. int cylinders = 8
    7. @Override
    8. String start() {
    9. }
    10. @Singleton
    11. class Vehicle {
    12. final Engine engine
    13. Vehicle(Engine engine) { (3)
    14. this.engine = engine
    15. }
    16. engine.start()
    17. }

    To perform dependency injection simply run the using the run() method and lookup a bean using getBean(Class), as per the following example:

    1. final BeanContext context = BeanContext.run();
    2. Vehicle vehicle = context.getBean(Vehicle.class);
    3. System.out.println(vehicle.start());
    1. val context = BeanContext.run()
    2. val vehicle = context.getBean(Vehicle::class.java)
    3. println(vehicle.start())

    Micronaut supports the following types of dependency injection:

    • Constructor injection (must be one public constructor or a single contructor annotated with )

    • Method parameter injection