{"id":16225739,"url":"https://github.com/macfja/injector","last_synced_at":"2025-09-17T21:18:40.196Z","repository":{"id":145406405,"uuid":"91618820","full_name":"MacFJA/Injector","owner":"MacFJA","description":"Classes injector. Injection can be done in constructors, setters, properties and methods","archived":false,"fork":false,"pushed_at":"2018-09-23T16:17:11.000Z","size":13,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-14T02:49:34.833Z","etag":null,"topics":["constructor-injection","injector","property-injection","setter-injection","singleton"],"latest_commit_sha":null,"homepage":null,"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/MacFJA.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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}},"created_at":"2017-05-17T20:48:09.000Z","updated_at":"2018-09-23T16:15:23.000Z","dependencies_parsed_at":"2023-06-03T17:15:24.806Z","dependency_job_id":null,"html_url":"https://github.com/MacFJA/Injector","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MacFJA%2FInjector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MacFJA%2FInjector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MacFJA%2FInjector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MacFJA%2FInjector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MacFJA","download_url":"https://codeload.github.com/MacFJA/Injector/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247773743,"owners_count":20993633,"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":["constructor-injection","injector","property-injection","setter-injection","singleton"],"created_at":"2024-10-10T12:46:07.080Z","updated_at":"2025-09-17T21:18:35.171Z","avatar_url":"https://github.com/MacFJA.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Injector\n\n - [Injection possibility](#injection)\n   - [Constructor Injection](#injection-constructor)\n   - [Setters Injection](#injection-setters)\n   - [Properties Injection](#injection-properties)\n   - [Method Injection](#injection-method)\n - [Injection types](#types)\n - [Installation](#installation)\n - [Examples](#examples)\n   - [Declaring a mapping](#examples-mapping)\n     - [For a singleton](#examples-mapping-singleton)\n     - [For an interface/abstract class to concrete implementation](#examples-mapping-interface)\n\n## Injection possibility\u003ca id=\"injection\"\u003e\u003c/a\u003e\n\nThis library offer several types of injection:\n\n- Constructors injection\n- Setters injection\n- Properties injection\n- Method injection\n\n### Constructor Injection\u003ca id=\"injection-constructor\"\u003e\u003c/a\u003e\n\nThe constructor injection try to create a class instance by looping over every class constructor until it found one that can be used .\n\nThe injection criteria are:\n\n- Parameters are not Java primitive\n- Parameters packages are in injector package list\n- All parameters constructor do the same\n\n### Setters Injection\u003ca id=\"injection-setters\"\u003e\u003c/a\u003e\n\nThe setter injection is automatically run after the constructor injection if the injector have the option activated.\n(Can be also be call on an existing instance)  \nFor a setter method to be injected, it must validate the following conditions:\n\n- The method name **MUST** start with `set`\n- The method **MUST** have exactly one parameter\n- The method **MUST** have the annotation `@javax.inject.Inject`\n- The method parameter must be an injectable class\n\n### Properties Injection\u003ca id=\"injection-properties\"\u003e\u003c/a\u003e\n\nThe property injection is automatically run after the constructor injection if the injector have the option activated.\n(Can be also be call on an existing instance)  \nFor a property to be injected, it must validate the following conditions:\n\n- The property **MUST** be accessible\n- The property **MUST** have the annotation `@javax.inject.Inject`\n- The property must be an injectable class\n\n### Method Injection\u003ca id=\"injection-method\"\u003e\u003c/a\u003e\n\nA method can have its parameters injected.\n\nThere are two way to inject parameters in a method.  \nFirst one is with a `java.lang.reflect.Method` object, in this case there are no control, if a parameter can't be injected `null` will be used.  \nThe second way is to use the method name, with this way, all method of the object with this name will be try, and the method must have the annotation `@javax.inject.Inject` and every parameters must be injectable.\n\n## Injection types\u003ca id=\"types\"\u003e\u003c/a\u003e\n\nThere are two injection types:\n\n- Singleton\n- Every times a new instance\n\n## Installation\u003ca id=\"installation\"\u003e\u003c/a\u003e\n\nClone the project:\n```\ngit clone https://github.com/MacFJA/Injector.git\n```\nInstall the project into your local Maven repository:\n```\ncd Injector/\nmvn clean\nmvn install\n```\nRemove the source:\n```\ncd ..\nrm -r Injector/\n```\nAdd the depency in your Maven project:\n```xml\n\u003cproject\u003e\n    \u003c!-- ... --\u003e\n    \u003cdependencies\u003e\n        \u003c!-- ... --\u003e\n        \u003cdependency\u003e\n            \u003cgroupId\u003eio.github.macfja\u003c/groupId\u003e\n            \u003cartifactId\u003einjector\u003c/artifactId\u003e\n            \u003cversion\u003e1.1.0\u003c/version\u003e\n        \u003c/dependency\u003e\n        \u003c!-- ... --\u003e\n    \u003c/dependencies\u003e\n    \u003c!-- ... --\u003e\n\u003c/project\u003e\n```\n\n## Examples\u003ca id=\"examples\"\u003e\u003c/a\u003e\n\n### Declaring a mapping\u003ca id=\"examples-mapping\"\u003e\u003c/a\u003e\n\n#### For a singleton\u003ca id=\"examples-mapping-singleton\"\u003e\u003c/a\u003e\n\n```java\nio.github.macfja.injector.Injector injector = new io.github.macfja.injector.Injector(\"mypackage\");\ninjector.addMapping(new mypackage.MyClass());\n// ... later\ninjector.get(mypackage.MyClass.class); // return the instance created in addMapping method\n// ... later\ninjector.get(mypackage.MyClass.class); // still the same instance\n```\n\nor\n\n```java\nio.github.macfja.injector.Injector injector = new io.github.macfja.injector.Injector(\"mypackage\");\ninjector.addMapping(mypackage.MyClass.class, new io.github.macfja.injector.InjectionUnit(new mypackage.MyClass()));\n// ... later\ninjector.get(mypackage.MyClass.class); // return the instance created in addMapping method\n// ... later\ninjector.get(mypackage.MyClass.class); // still the same instance\n```\n\nor\n\n```java\nio.github.macfja.injector.Injector injector = new io.github.macfja.injector.Injector(\"mypackage\");\ninjector.addMapping(\n    mypackage.MyClass.class,\n    new io.github.macfja.injector.InjectionUnit(\n        mypackage.MyClass.class,\n        io.github.macfja.injector.InjectionUnit.Instantiation.Singleton\n    )\n);\n// ... later\ninjector.get(mypackage.MyClass.class); // create a new instance (first call)\n// ... later\ninjector.get(mypackage.MyClass.class); // still the same instance\n```\n\nor\n\n```java\nio.github.macfja.injector.Injector injector = new io.github.macfja.injector.Injector(\"mypackage\");\ninjector.addMapping(\n    mypackage.MyClass.class,\n    io.github.macfja.injector.InjectionUnit.Instantiation.Singleton\n);\n// ... later\ninjector.get(mypackage.MyClass.class); // create a new instance (first call)\n// ... later\ninjector.get(mypackage.MyClass.class); // still the same instance\n```\n\n#### For an interface/abstract class to concrete implementation\u003ca id=\"examples-mapping-interface\"\u003e\u003c/a\u003e\n\n```java\nio.github.macfja.injector.Injector injector = new io.github.macfja.injector.Injector(\"mypackage\");\ninjector.addMapping(mypackage.MyInterface.class, new io.github.macfja.injector.InjectionUnit(/* ... */));\n// ... later\ninjector.get(mypackage.MyInterface.class); // return an instance according to the InjectionUnit\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmacfja%2Finjector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmacfja%2Finjector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmacfja%2Finjector/lists"}