{"id":14972926,"url":"https://github.com/spring-projects/spring-guice","last_synced_at":"2025-04-06T02:09:03.606Z","repository":{"id":16111673,"uuid":"18856776","full_name":"spring-projects/spring-guice","owner":"spring-projects","description":"Tools for using Spring in Guice and Guice in Spring","archived":false,"fork":false,"pushed_at":"2024-08-05T16:27:51.000Z","size":371,"stargazers_count":176,"open_issues_count":7,"forks_count":66,"subscribers_count":16,"default_branch":"main","last_synced_at":"2025-03-30T01:06:44.055Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spring-projects.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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":"2014-04-16T21:27:08.000Z","updated_at":"2025-03-21T06:28:21.000Z","dependencies_parsed_at":"2024-08-05T19:11:48.398Z","dependency_job_id":null,"html_url":"https://github.com/spring-projects/spring-guice","commit_stats":{"total_commits":180,"total_committers":23,"mean_commits":7.826086956521739,"dds":0.7333333333333334,"last_synced_commit":"498fed04ab900b57237dda9936e41b95c4750303"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spring-projects%2Fspring-guice","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spring-projects%2Fspring-guice/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spring-projects%2Fspring-guice/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spring-projects%2Fspring-guice/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spring-projects","download_url":"https://codeload.github.com/spring-projects/spring-guice/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247423515,"owners_count":20936626,"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":[],"created_at":"2024-09-24T13:47:46.160Z","updated_at":"2025-04-06T02:09:03.576Z","avatar_url":"https://github.com/spring-projects.png","language":"Java","readme":"This project provides bridges between Spring and Guice so that you can\nuse one from the other (and vice versa). It works with Spring 6 (or at least the tests are green), but Guice does [not support the Jakarta annotations](https://github.com/google/guice/issues/1383) so it may break in unexpected ways.\n\n![Build Status](https://travis-ci.org/spring-projects/spring-guice.svg?branch=master)\n\n## Using a Spring ApplicationContext as a Module in Guice\n\nThe main bridge in this case is a Guice `Module` that wraps an\nexisting Spring `ApplicationContext`. Example:\n\n```java\nAnnotationConfigApplicationContext context = \n    new AnnotationConfigApplicationContext(ApplicationConfiguration.class);\nInjector injector = Guice.createInjector(new SpringModule(context), new MyModule());\nService service = injector.getInstance(Service.class);\n```\n\n`SpringModule` (`org.springframework.guice.module.SpringModule`) will wrap the existing spring configurations for you.\n\nNote that the `ApplicationContext` in this example might contain the\n`Service` definition or it might be in the Guice `Module`\n(`MyModule`), or if `Service` is a concrete class it could be neither,\nbut Guice creates an instance and wires it for us.\n\nIf the `ApplicationConfiguration` is annotated `@GuiceModule` then it\ncan filter the types of bean that are registered with the Guice\nbinder. Example:\n\n```java\n@Configuration\n@GuiceModule(includeFilters=@Filter(pattern=.*\\\\.Service))\npublic class ApplicationConfiguration {\n    @Bean\n    public MyService service() {\n        ...\n    }\n}\n```\n\nIn this case, only bean types (or interfaces) called \"Service\" will\nmatch the include filter, and only those beans will be bound.\n\nIf there are multiple `@Beans` of the same type in the\n`ApplicationContext` then the `SpringModule` will register them all,\nand there will be a runtime exception if an `Injector` needs one. As\nwith normal Spring dependency resolution, you can add the `@Primary`\nmarker to a single bean to differentiate and hint to the `Injector`\nwhich instance to use.\n\n## Registering Spring Configuration Classes as a Guice Module\n\nIf your Spring `@Configuration` has dependencies that can only come\nfrom a Guice `Module` and you prefer to use the Guice APIs to build up\nthe configuration (so you can't use `@EnableGuiceModules` below), then\nyou can create a `SpringModule` from a\n`Provider\u003cConfigurableListableBeanFactory\u003e` instead of from an\nexisting `ApplicationContext`. There are some additional features that\nmay also apply:\n\n* If the bean factory created by the provider is a\n`DefaultListableBeanFactory` (mostly it would be if it came from an\n`ApplicationContext`), then it will pick up a special Guice-aware\n`AutowireCandidateResolver`, meaning that it will be able to inject\ndependencies from Guice modules that are not registered as beans.\n\n* If the bean factory contains any beans of type `ProvisionListener`\n(a Guice lifecysle listener), then those will be instantiated and\nregistered with Guice.\n\nTo take advantage of the autowiring the bean factory must come from an\n`ApplicationContext` that is not fully refreshed (refreshing would\nresolve all the dependencies and fail because the Guice resolver is\nnot yet registered). To help you build bean factories that have this\nquality there is a convenience class called `BeanFactoryProvider` with\nstatic methods which you can use to create a provider to inject into a\n`SpringModule`.  Example:\n\n```java\nInjector injector = Guice.createInjector(new SimpleGuiceModule(), \n    new SpringModule(BeanFactoryProvider.from(SpringConfiguration.class)));\n```\n\nThe `SimpleGuiceModule` contains a component that the\n`SpringConfiguration` depends on.\n\n## Using existing Guice Modules in a Spring ApplicationContext\n\nThe main feature here is a Spring `@Configuration` annotation:\n`@EnableGuiceModules`. If you have Guice `Modules` that you want to\nre-use (e.g. if they come from a third party) you can declare them in\na Spring `ApplicationContext` as `@Beans`, and expose all their\nbindings. Example:\n\n```java\n@EnableGuiceModules\n@Configuration\npublic static class TestConfig {\n\n    @Bean\n    public static MyModule myModule() {\n        return new MyModule();\n    }\n\n    @Bean\n    public Spam spam(Service service) {\n        return new Spam(service);\n    }\n\n}\n```\n\nThe `Service` was defined in the Guice module `MyModule`, and then it\nwas be bound to the autowired `spam()` method when Spring started.\n\n### Filtering out modules from startup of ApplicationContext\n\nIn certain cases you might need to ensure that some modules are not configured at all, even though they will be absent from the final `ApplicationContext`.\nThis might be due to external code that may be hard to change that causes side effects at binding time, for example.\nTo ensure that these modules are not called at all you can define one or more`ModuleFilter` beans that will be applied to filter out modules from the context before they are touched by the Spring-Guice bridge.\n\n```java\n\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.guice.annotation.ModuleFilter;\n\npublic static class TestConfig {\n\n\n  @Bean\n  public static MyModule myModule() {\n    return new MyModule();\n  }\n\n  @Bean\n  public ModuleFilter myFilter() {\n    return module -\u003e !(module instanceof MyModule);\n  }\n}\n\n```\nThis will ensure that no `configure()` methods are called on the filtered modules.\n\n## Configuration Class Enhancements\n\nNote that the `Module` bean definition in the example above is \ndeclared in a `static` method. This is intentional and can be used to\navoid accidentally preventing Spring from being able to enhance the\nparent `@Configuration` class. The default behaviour for Spring is to\ncreate a proxy for `@Configuration` classes so the `@Bean` methods\ncan call each other and Spring will preserve the singleton nature of\nthe bean factory - one bean of each type per bean id.\n\nIf you see logs like this when the context starts:\n\n```\nMar 21, 2022 8:30:35 AM org.springframework.context.annotation.ConfigurationClassPostProcessor enhanceConfigurationClasses\nINFO: Cannot enhance @Configuration bean definition 'TestConfig' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'.\n```\n\nthat is a sign that you might want to use static methods to define\nany beans of type `Module`. It is logged at INFO because Spring \ndoesn't know if it was intentional or not. Most likely it was\nunintentional, and it is better to avoid nasty surprises later \nif we can.\n\nAnother way to avoid the warning is to declare the parent class as \n`@Configuration(proxyBeanMethods = false)` so that Spring knows that\nyou don't even want to enhance the class. It's not a bad idea to use\nthat flag wherever you can because it saves some time on start up\nif the proxy doesn't need to be created.\n\n## Using Guice as an API for accessing a Spring ApplicationContext\n\nIn this case the main feature is an `Injector` implementation that\nwraps a Spring `ApplicationContext`. Example:\n\n```java\nAnnotationConfigApplicationContext context = \n    new AnnotationConfigApplicationContext(ApplicationConfiguration.class);\nInjector injector = new SpringInjector(context);\nService service = injector.getInstance(Service.class);\n```\n\nIf there is a `@Bean` of type `Service` it will be returned from the\n`Injector`. But there may actually not be a `@Bean` definition of type\n`Service`, and if it is a concrete type then the `Injector` will\ncreate it and autowire its dependencies for you. A side effect of this\nis that a `BeanDefinition` *will* be created.\n\nIn the example above, if `ApplicationConfiguration` was annotated\n`@EnableGuiceModules` then there is an `Injector` bean already waiting\nto be used, including wiring it into the application itself if you\nneed it. So this works as well:\n\n```java\n@Configuration\n@EnableGuiceModules\npublic class ApplicationConfiguration {\n\n    @Autowired\n    private Injector injector;\n    \n    @Bean\n    public Foo foo() {\n        // Guice creates and does the wiring of Foo instead of Spring\n        return injector.getInstance(Foo.class);\n    }\n}\n```\n\nIn this example if the `Injector` has a binding for a `Provider` of\n`Foo.class` then the `foo()` method is redundant - it is already\nresolvable as a Spring dependency. But if Guice is being used as a\nfactory for new objects that it doesn't have bindings for, then it\nmakes sense.\n\n**Note:** if you also have `@GuiceModule` in your context, then using\nthe injector to create a `@Bean` directly is a bad idea (there's a\ndependency cycle). You *can* do it, and break the cycle, if you\nexclude the `@Bean` type from the `Injector` bindings using the\n`@GuiceModule` exclude filters.\n\n## Configurable Options\n\nFor a full list of configuration options, see the [configuration metadata file](https://github.com/spring-projects/spring-guice/blob/master/src/main/resources/META-INF/additional-spring-configuration-metadata.json).\n\n**Binding Deduplication** - When using `@EnableGuiceModules`, if a Spring `Bean` and a Guice `Binding` both exist for the same type and `Qualifier`, creation of the `Injector` will fail. You may instead prefer to keep Spring's instance of the type instead of receiving this error. To accomplish this, you may set the property `spring.guice.dedup=true`.\n\n**Disable Guice just-in-time bindings** - When enabled (default enabled), beans without explicit definitions will be created using Guice just-in-time bindings. Otherwise, it will fail with UnsatisfiedDependencyException. To disable, set the property `spring.guice.autowireJIT=false`.\n\n## Limitations\n\n* So far there is no support for the Guice SPI methods in\n  `SpringInjector` so tooling may not work. It wouldn't be hard to do.\n\n* `SpringInjector` only knows about raw types and bean names, so it\n  ignores additional meta-information in factory requests (like\n  annotations other than `@Named`). Should be easy enough to fix, but\n  some compromises might have to be made.\n  \n* `SpringInjector` has no support for creating child or parent\n  `Injectors`. Probably not difficult.\n\n* `SpringModule` treats all beans as singletons.\n\n* `SpringModule` binds all interfaces and all names of a bean it can\n  find. This should work out OK, as long as those interfaces are not\n  needed for injection (and if there is no `@Primary` bean).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspring-projects%2Fspring-guice","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspring-projects%2Fspring-guice","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspring-projects%2Fspring-guice/lists"}