I have services developed using Google guice DI framework and I want to inject them in spring boot application. Could anybody thro some light on how we can access the google guice based services in spring boot application? Any pointers to any sample code?
-
1Check this link https://stackoverflow.com/questions/21056063/spring-and-guice-together-or-just-spring – Ng Sharma Feb 28 '19 at 05:09
-
I already have legacy guice services and ]hose services can not be duplicated in spring. – Madhu Feb 28 '19 at 07:18
1 Answers
Welcome to StackOverflow!
There are two different things that you could do, depending on what you actually did in the Guice services.
If you used only
JSR330
annotations, then you could theoretically simply replace Guice Modules with Spring@Configuration
classes and just use Spring to do the D.I. This would work because both Spring and Guice support theJSR330
annotations. In my view, this can work in only fairly simple cases.Use the
org.springframework.guice:spring-guice
library from Spring. It allows you to use Guice Modules and the provided beans.
First you need to include it in your pom:
<dependency>
<groupId>org.springframework.guice</groupId>
<artifactId>spring-guice</artifactId>
<version>1.1.3.RELEASE</version>
</dependency>
Then you must configure Spring to use the needed Guice Modules:
@Configuration
@EnableGuiceModules
public static class GuiceConfig {
@Bean
public MyModule myModule() {
return new MyModule();
}
}
You can read more on the GitHub for this project: https://github.com/spring-projects/spring-guice#using-existing-guice-modules-in-a-spring-applicationcontext.

- 5,127
- 2
- 17
- 34
-
Can I install Guice external legacy guice module here in MyModule as below? protected static class MyModule extends AbstractModule { @Override protected void configure() { install(new LogParserGuiceModule()); } } and invoke the bean like: LogParserService sr = context.getBean(LogParserServiceImpl.class); – Madhu Mar 03 '19 at 10:38
-
As long as your module is instantiated as a `@Bean` in a Spring `@Configuration`, then you should be able to access all the services provided in that module through the regular Spring mechanisms (injection via `@Autowired`, retrieving from the application context, etc). – Serban Petrescu Mar 04 '19 at 05:40