{"id":20855982,"url":"https://github.com/henryco/grinjector","last_synced_at":"2025-12-26T11:55:19.266Z","repository":{"id":57719938,"uuid":"114365691","full_name":"henryco/GrInjector","owner":"henryco","description":"Lightweight reflective dependency injector for java.","archived":false,"fork":false,"pushed_at":"2019-05-17T21:36:19.000Z","size":232,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-19T07:09:54.665Z","etag":null,"topics":["dependency-injection","java","java-8","library","reflection"],"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/henryco.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-12-15T11:57:45.000Z","updated_at":"2019-05-17T21:36:20.000Z","dependencies_parsed_at":"2022-09-26T22:30:35.672Z","dependency_job_id":null,"html_url":"https://github.com/henryco/GrInjector","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henryco%2FGrInjector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henryco%2FGrInjector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henryco%2FGrInjector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henryco%2FGrInjector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/henryco","download_url":"https://codeload.github.com/henryco/GrInjector/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243230067,"owners_count":20257640,"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","java","java-8","library","reflection"],"created_at":"2024-11-18T04:27:52.490Z","updated_at":"2025-12-26T11:55:19.217Z","avatar_url":"https://github.com/henryco.png","language":"Java","readme":"# GrInjector [![Maven Central](https://img.shields.io/maven-central/v/com.github.henryco/grinjector.svg)](http://repo1.maven.org/maven2/com/github/henryco/grinjector/)  [![GitHub license](https://img.shields.io/badge/license-MIT-yellow.svg)](https://raw.githubusercontent.com/henryco/GrInjector/master/LICENSE)\n#### Lightweight reflective dependency injector for java and Android.  \n\n  ____\n  \n  \n**Gradle**  \n```Groovy\n    compile 'com.github.henryco:grinjector:1.2.1'\n```  \n**Maven**  \n```XML\n    \u003cdependency\u003e\n        \u003cgroupId\u003ecom.github.henryco\u003c/groupId\u003e\n        \u003cartifactId\u003egrinjector\u003c/artifactId\u003e\n        \u003cversion\u003e1.2.1\u003c/version\u003e\n    \u003c/dependency\u003e\n```\n\n____\n\n\n# **About**\n\n\u003e ##### **This is guide for version `1.2.+` for version `1.1.0` and below see [this](https://bitbucket.org/tinder-samurai/grinjector/wiki/Version%201.1.0%20and%20below) .**\n\n**GrInjector** is simple reflective dependency injector for Java and Android that provides out of the box all functionality you may need. The whole user part of library contains a small number of annotations:\n\n* **`@Module`**\n* **`@Provide`**\n* **`@TargetInterface`**\n\nplus standard [**`JSR-330`**](https://docs.oracle.com/cd/E19798-01/821-1841/gjxvg/index.html) annotations from package [**`javax.inject`**](https://mvnrepository.com/artifact/javax.inject/javax.inject/1) which are:\n\n* **`@Named`**\n* **`@Inject`**\n* **`@Singleton`**\n\n\neach of them will be described below.  \n  \n ****\n  \n# **Module declaration**\nThe first thing you should know is that the dependency injector needs to declare at least one root module. In order to declare a class a module, it's enough just to add the **`@Module`** annotation. Modules can also include other modules and get dependencies on them, for this case annotation has field: \n**`Class\u003c?\u003e[] include() default {}; `**\n\n\n```java\n\n@Module (\t\t\t// \u003c- This annotation marks class as dependency module\n\tinclude = ModuleGE.class // \u003c- Include statement adds child modules\n) public class ModuleGC {\n\n\n\t@Provide(\"prv_not_com_a\")\n\tpublic NotAnnotatedComponentA prvCompA(@Named(\"strArr\") String[] arr,\n\t\t\t\t\t\t\t\t\t\t   List\u003cInteger\u003e list) {\n\t\treturn new NotAnnotatedComponentA(arr, (ArrayList\u003cInteger\u003e) list);\n\t}\n\n\n\t@Provide(\"strArr\")\n\tpublic String[] provideStringArray() {\n\t\treturn new String[]{\"1\", \"2\", \"3\", \"4\"};\n\t}\n\n\n\t@Provide @Singleton\n\tpublic float provideFloatB(Scanner scanner) {\n\t\treturn 23;\n\t}\n\n}\n\n```\n\n\n\nThe necessary module, before it will be used, should be added to the main library of dependencies of the library via static method call **`GrInjector.addModules()`** as example is shown below.\n\n```java\npublic final class SomeMainClass {\n\n\tpublic static void main(String[] args) {\n\t\tGrInjector.addModules(ModuleGA.class);\n\t\t//Some application code below\n\t}\n}\n```\n\n\n****\n\n# **Dependency recipients**\nTo comply with the control inversion, that where it is necessary to introduce dependence should indicate where these dependencies are created and processed - in the module. For this, the annotation has fields: \n```java \nClass\u003c?\u003e[] targets() default {};           // eg. {ClassA.class, ClassB.class, ClaccC.class}\nClass\u003c?\u003e[] targetsRootClass() default {};  // eg. net.henryco.some.package.SomeClass.class\nString[] targetsRootPath() default {};     // eg. \"net.henryco.some.package\"\n```\nInstead of the fields listed above, you can use the **`@TargetInterface`** annotation which should note the interface in which dependency recipients are specified. The class of this interface should be passed to the module in the **`@Module`** annotation through its **`targets`** field. In order to specify the dependency receiver(s) in the interface, you should create a method marked with an **`@Inject`** annotation, in which case the recipients (targets) well be arguments of this method. The number of methods, as well as their names, doesn't matter.\nAn example of such an interface below.\n```java\n@TargetInterface\npublic interface ExampleTargetInterface {\n\n\t@Inject void inject(\n\t\tSomeControllerA target1,\n\t\tSomeControllerB target2\n\t\t// .\n\t\t// .\n\t\t// .\n\t\t// Other targets\n\t);\n}\n```\n\n\n****\n\n# **Providing dependencies (beans)**\nTo create your own bean, simply create a method in the module and mark it with **`@Provide`** annotation. Such a method can have arguments - dependencies that will be automatically provided to it. You should be careful to avoid self injecting otherwise you will fall into an infinite loop of recursive self-instancing.\n```java\n@Module(targets = {SomeTarget.class})\npublic final class SomeModule {\n\n        @Provide // bean will have method name which is: \"provideNumberString\"\n        public String provideNumberString(Long number) { // argument will be automatically injected\n                return \"number: \" + number.toString();\n        }\n        \n        @Provide(\"abc\") // bean will have name \"abc\"\n        public int abcBean() {\n                return 42;\n        }\n}\n```\n\n****\n\n# **Components binding**\nComponents are like beans that already defined outside the module (and also can be can be marked with **`@Singleton`**). GrInjector allows you to automatically create and link components and provide them required dependencies. For this, the **`class`** component should be marked with the **`@Provide`** annotation, and added to the module. For this case, **`@Module`** annotation has the following fields:\n```java\nClass\u003c?\u003e[] components() default {};\nClass\u003c?\u003e[] componentsRootClass() default {};\nString[] componentsRootPath() default {};\n```\n\n****\n\n# **Singletons**\nTo make the necessary component (or bean) a singleton, it's enough to mark it with **`@Singleton`** annotation and its instance will be created only once.\n\n\n****\n\n# **Injection**\n**`GrInjector`** library allows dependency injection via **`FIELD`**, **`SETTER`** or **`CONSTRUCTOR`**, you just need to add an **`@Inject`** annotation and library will resolve required instance by type or name, automatically. Pay attention, that injection via **`CONSTRUCTOR`** works only with components, that's because components are created and stored in a dependency container, in other cases for injection you should add target class to module and then call the static method **`GrInjector.inject()`** in that class, also you can just get single component by calling method **`GrInjector.getComponent();`**. Since version **`1.2.0`** you **should** use **`@Named`** annotation with methods, fields and constructors parameters to resolve required dependencies by their names, like: \n```java\n@Inject\npublic SomeConstructor(@Named(\"wow\") String wow) { ... }\n```\n\n\n****\n\n# **Custom IClassFinder implementation**\nSometimes there is a need to use own implementation of the class search engine, since the standard implementation is not able to find classes in extraneous jar's. In this case, the library contains the interface IClassFinder, the implementation of which should be passed to the method:\n\n```java\nGrInjector.addModules(IClassFinder imp, Class\u003c?\u003e[] modules);\n\n```\n**`IClassFinder`** interface contains only one method which means that you can use a lambda expression:\n```java\npackage com.github.henryco.injector.meta.resolver;\nimport java.util.List;\n\n@functionalinterface \npublic interface IClassFinder {\n\tList\u003cClass\u003c?\u003e\u003e getClassesForPackage(String pckgname) throws ClassNotFoundException;\n}\n```\n\n\n  ****\n\n# **Full code example**\n\n```java\n@Module(targets = {SomeController.class})\npublic class SomeModule {\n\t// Beans and components here\n}\n\n@Singleton\n@Provide(\"some_comp\")\npublic class SomeComponent {\n       \n        private final Sting abc;\n        \n        @Inject // By constructor and type\n        public SomeComponent(String text) {\n                this.abc = text;\n        }\n}\n\npublic class SomeController {\n\n        // By field, resolved by name\n\t@Inject @Named(\"some_comp\") \t\n\tprivate SomeComponent comp;\n\n\tprivate float someValue;\n\n\tpublic SomeController() {\n\t\tinit();\n\t}\n\n\tprivate void init() {\n                \n                // Inject via @Inject annotations\n\t\tGrInjector.inject(this);\n\n                // Get component manually from dependency container\n                OtherComponent other = GrInjector.getComponent(\"component_name\");\n\t\t\n                // some code\n\t}\n\n\t@Inject // By Setter, resolved by type\n\tpublic void setSomeValue(float someValue) {\n\t\tthis.someValue = someValue;\n\t}\n\n}\n\npublic class SomeMain {\n        public static void main(String[] args) {\n                GrInjector.addModules(SomeModule.class);\n\t\tSomeController controller = new SomeController();\n\t\t\n                // some code\n\t}\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhenryco%2Fgrinjector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhenryco%2Fgrinjector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhenryco%2Fgrinjector/lists"}