Skip to content

Latest commit

 

History

History
86 lines (56 loc) · 3.1 KB

File metadata and controls

86 lines (56 loc) · 3.1 KB

simple-component-compiler simple-component

Coffee time

class CoffeeApp {

    @Component interface CoffeeComponent {
        CoffeeMaker coffeeMaker();

        @Component.Builder interface Builder {
            Builder logLevel(String logLevel);
            CoffeeComponent buildComponent();
        }
    }

    interface Logger {
        void log(String msg);
    }

    static class CoffeeMaker {
        private final Logger logger;

        @Inject CoffeeMaker(Logger logger) {
            this.logger = logger;
        }

        void brew() {
            logger.log("~ ~ ~ heating ~ ~ ~");
            logger.log("=> => pumping => =>");
            logger.log(" [_]P coffee! [_]P ");
        }
    }

    @Inject static Logger createLogger(String level) {
        return msg -> System.out.println(level + " " + msg);
    }
}

This dependency injector uses the following annotations:

  1. @Inject declares an injection point. It can be a constructor or a static method in the bean class. It can also be a static method in the component class.
  2. @Qualifier and its default implementation @Named.
  3. And of course, @Component, @Component.Factory and @Component.Builder.

Note this is not a complete implementation of javax.inject or jakarta.inject, because:

The @Scope and @Singleton annotations are ignored.

Instead there's the following rule:

If two beans of the same type and same qualifier are injected by the same component, then they are the same instance.

Intuitively this means the same bean instance is injected everywhere (unless you're using qualifiers, or inject a provider).

If you want to re-use a bean instance across multiple components, or multiple instances of the same component, pass it via a @Factory or a @Builder.

If you inject Provider<TheBean> provider, calling provider.get() will create a fresh bean instance every time.

Use case?

There are no "subcomponents" or "component dependencies".

There is no @Module.

There is no @Binds. Use a @Provides method.

There is no Provider. Create a factory if you need one.

There is no @IntoList or @IntoSet. Collections can be assembled in a @Provides method.

There is no @Lazy, @BindsInstance or @AssistedInject. I don't even know what these do.

Samples

Alternatives