{"id":13515256,"url":"https://github.com/FunnyGuilds/dependency-injector","last_synced_at":"2025-03-31T04:36:46.480Z","repository":{"id":39980604,"uuid":"281534426","full_name":"FunnyGuilds/dependency-injector","owner":"FunnyGuilds","description":"Blazingly fast and lightweight dependency injection framework for Java 💉","archived":false,"fork":false,"pushed_at":"2025-03-14T17:43:19.000Z","size":445,"stargazers_count":18,"open_issues_count":7,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-23T11:03:52.566Z","etag":null,"topics":["dependency-injection","dependency-injection-framework","panda-lang"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/FunnyGuilds.png","metadata":{"files":{"readme":".github/README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"dzikoysk","custom":"https://panda-lang.org/support"}},"created_at":"2020-07-22T00:30:08.000Z","updated_at":"2024-11-04T11:11:38.000Z","dependencies_parsed_at":"2023-10-05T06:28:15.183Z","dependency_job_id":"69213ead-254f-43a0-b93b-6105beb6a7b4","html_url":"https://github.com/FunnyGuilds/dependency-injector","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FunnyGuilds%2Fdependency-injector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FunnyGuilds%2Fdependency-injector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FunnyGuilds%2Fdependency-injector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FunnyGuilds%2Fdependency-injector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FunnyGuilds","download_url":"https://codeload.github.com/FunnyGuilds/dependency-injector/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246418658,"owners_count":20773934,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dependency-injection","dependency-injection-framework","panda-lang"],"created_at":"2024-08-01T05:01:08.425Z","updated_at":"2025-03-31T04:36:41.467Z","avatar_url":"https://github.com/FunnyGuilds.png","language":"Java","funding_links":["https://github.com/sponsors/dzikoysk","https://panda-lang.org/support"],"categories":["Java"],"sub_categories":[],"readme":"# Dependency Injection [![CI](https://github.com/dzikoysk/dependency-injector/actions/workflows/gradle.yml/badge.svg)](https://github.com/dzikoysk/dependency-injector/actions/workflows/maven.yml) [![codecov](https://codecov.io/gh/FunnyGuilds/dependency-injector/branch/master/graph/badge.svg)](https://codecov.io/gh/FunnyGuilds/dependency-injector) ![maven](https://maven.reposilite.com/api/badge/latest/releases/org/panda-lang/utilities/di?color=40c14a\u0026name=Latest%20Release\u0026prefix=v)\n\nBlazingly fast and lightweight dependency injection framework for Java. Supported operations:\n* Creating a new instance of the specified type using Injector _(constructors)_\n* Invoking methods using Injector _(methods)_\n* Reflection based field injection during instance creation _(fields)_\n\n### Install\nLibrary is available in `panda-repository`:\n\n```xml\n\u003crepository\u003e\n    \u003cid\u003epanda-repository\u003c/id\u003e\n    \u003curl\u003ehttps://maven.reposilite.com/releases\u003c/url\u003e\n\u003c/repository\u003e\n```\n\nTo use `annotations`, declare the dependency in your `pom.xml`\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.panda-lang.utilities\u003c/groupId\u003e\n    \u003cartifactId\u003edi\u003c/artifactId\u003e\n    \u003cversion\u003e1.8.0\u003c/version\u003e\n\u003c/dependency\u003e\n\n\u003c!-- Codegen module --\u003e\n\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.panda-lang.utilities\u003c/groupId\u003e\n    \u003cartifactId\u003edi-codegen\u003c/artifactId\u003e\n    \u003cversion\u003e1.8.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n### Usage\nFirstly, you have to create `Injector` which keeps all the registered bindings (injected values)\n\n```java\nInjector injector = DependencyInjection.createInjector()\n\n// If you are able to register binding at the init time, it's also recommended to use the following structure\nInjector injector = DependencyInjection.createInjector(resources -\u003e {\n    // bind resources\n});\n```\n\nInjector supports three main ways to bind with a value:\n* Binding to the specified type `resources.on(\u003cType\u003e)`\n* Binding to the specified annotation `resources.annotatedWith(\u003cAnnotation\u003e)`\n* Binding to the verified annotation `resources.annotatedWithTested(\u003cAnnotation\u003e)` *(safer but slower alternative to `annotatedWith`)*\n\nEach binding supports three ways of assigning value:\n* `assignInstance(\u003cObject\u003e)`/`assignInstance(Supplier\u003cObject\u003e)` - binds the specified value/some kind of lazy values\n* `assignHandler((\u003cExpected Type Of Value\u003e, \u003cAnnotation\u003e) -\u003e { /* logic */ })` - binds custom handler\n\n#### Instances\nThe most common use of DI comes down to some kind of instance assignation:\n\n```java\npublic static class Bean { }\n\npublic interface Custom { }\nstatic class CustomImpl implements Custom { }\n\nstatic class Service {\n    public Service(Bean bean, Custom custom) {\n        assertNotNull(bean);\n        assertNotNull(custom);\n    }\n}\n```\n\nTo create `Service` class, just invoke its constructor:\n\n```java\nInjector injector = DependencyInjection.createInjector();\n\n// some logic, a few hours later...\n\ninjector.getResources().on(Custom.class).assignInstance(new CustomImpl()); // singleton\ninjector.getResources().on(Bean.class).assignInstance(Bean::new); // new instance per call\n\nService service = injector.forConstructor(Service.class).newInstance();\n```\n\nFull example: [DependencyInjectionInstancesTest.java](https://github.com/FunnyGuilds/dependency-injector/blob/master/di/src/test/java/org/panda_lang/utilities/inject/DependencyInjectionInstancesTest.java)\n\n#### Fields\n```java\nclass Service {\n    @Inject\n    private String fieldOne;\n    @Inject\n    private Integer fieldTwo;\n}\n\nInjector injector = DependencyInjection.createInjector(resources -\u003e {\n    resources.on(String.class).assignInstance(\"Hello Field\");\n    resources.on(Integer.class).assignInstance(7);\n});\n\nService service = injector.newInstanceWithFields(Service.class);\n```\n\nFull example: [DependencyInjectionFieldsTest.java](https://github.com/FunnyGuilds/dependency-injector/blob/master/di/src/test/java/org/panda_lang/utilities/inject/DependencyInjectionFieldsTest.java)\n\n\n#### Custom logic\n\nLet's build a random example based on these methods using a custom annotation:\n\n```java\n@Injectable // mark annotation as DI ready annotation\n@Retention(RetentionPolicy.RUNTIME) // make sure that the annotation is visible at runtime\n@interface AwesomeRandom { }\n\nstatic final class Entity {\n    private final UUID id;\n\n    private Entity(@AwesomeRandom UUID random) {\n        this.id = random;\n    }\n\n    public UUID getId() {\n        return id;\n    }\n}\n```\n\nWe'd like to generate a new id per each `Entity` instance with a private constructor. It's also important for us, to support id in two forms:\n* String\n* UUID\n\n```java\nInjector injector = DependencyInjection.createInjector(resources -\u003e {\n    resources.annotatedWith(AwesomeRandom.class).assignHandler((expectedType, annotation) -\u003e {\n        if (expectedType.equals(String.class)) {\n            return UUID.randomUUID().toString();\n        }\n\n        if (expectedType.equals(UUID.class)) {\n            return UUID.randomUUID();\n        }\n\n        throw new IllegalArgumentException(\"Unsupported type \" + expectedType);\n    });\n});\n\n// Create entities using the injector instance\nEntity entityA = injector.newInstance(Entity.class);\nEntity entityB = injector.newInstance(Entity.class);\n\n// Print generated values\nSystem.out.println(entityA.getId());\nSystem.out.println(entityB.getId());\n```\n\nThe output produces some random identifiers as intended 👍 \n\n```\ne23442b2-f695-41fa-9290-0f1192118a1a\n9f92121c-096e-4bdb-b6ad-0901974bbe37\n\nProcess finished with exit code 0\n```\n\nFull example is available here -\u003e [DependencyInjectionWikiTest.java](https://github.com/FunnyGuilds/dependency-injector/blob/master/di/src/test/java/org/panda_lang/utilities/inject/DependencyInjectionWikiTest.java)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FFunnyGuilds%2Fdependency-injector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FFunnyGuilds%2Fdependency-injector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FFunnyGuilds%2Fdependency-injector/lists"}