GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - jbock-java/simple-component: Basic dependency injection tool

BranchesTags
Open more actions menu

Latest commit

 

History

261 Commits
261 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
compiler
compiler
 
 
gradle/wrapper
gradle/wrapper
 
 
simple-component
simple-component
 
 
.gitignore
.gitignore
 
 
README.md
README.md
 
 
gradlew
gradlew
 
 
install_local
install_local
 
 
release
release
 
 
settings.gradle.kts
settings.gradle.kts
 
 

Repository files navigation

simple-component-compiler simple-component

Coffee time

class CoffeeApp {

    @Component
    interface CoffeeComponent {
        CoffeeMaker coffeeMaker();

        @Component.Factory
        interface MyFactory {
            CoffeeComponent buildComponent(Logger logger);
        }
    }

    interface Logger {
        void log(String msg);
    }

    @Inject
    record CoffeeMaker(Logger logger) {

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

(source)

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

  • modular-thermosiphon (dagger's "coffee machine" demo)
  • jbock uses it, see for example ValidateComponent

Alternatives

  • https://github.com/google/dagger/
  • https://github.com/avaje/avaje-inject
  • https://github.com/michaelboyles/simple-di

About

Basic dependency injection tool

Topics

annotation-processordaggerdependency-injectionjava

Resources

Readme
Activity
Custom properties

Stars

8 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.