Integrate Guice Component into Spring Application

We have an application with the Spring framework and it is necessary to integrate the component created using Google Guice.

Can someone give us some tips on how to do this?

We came across the following links that show how to integrate Spring into Guice, but we need it the other way around:

http://google-guice.googlecode.com/git/javadoc/com/google/inject/spring/SpringIntegration.html

http://www.jroller.com/mindcrime/entry/an_example_of_integrating_guice

Any help is appreciated

+8
source share
3 answers

No Special structure or dependency is required to integrate the guice component into spring-boot.

standard boot application

@SpringBootApplication public class App { SpringApplication.run(App.class, appArgs); } 

Guice Module Configuration

  @Configuration public class GuiceBeanConfig { private final Injector injector; // guice modules are initialized before the spring context completes { injector = Guice.createInjector( new MyModuleA(), new MyModuleB() ); } /** * Option1: can expose injector as a Spring Bean. */ @Bean public Injector injector() { return injector; } /** * Option2: expose specific component as a Spring Bean. */ @Bean public MyServiceA serviceA() { return injector.getInstance(ServiceA.class); } } 

Automatically connect your service component as a regular spring component.

  • Option 1: Guice automatic injector and access to any bean from it

      @Service public class MySpringService { private final Injector injector; MySpringService (Injector i){ injector = i; } public void callServiceA() { injector.getInstance(ServiceA.class).doSomething(); } } 
  • Option 2: special bin with auto

     @Service public class MySpringService { private final ServiceA serviceA; MySpringService (ServiceA s){ serviceA = s; } public void callServiceA() { serviceA.doSomething(); } } 

NOTE: Spring-boot does not require the use of @Autowire annotation in case you make a constructor injector.

+1
source

Take a look at the spring -guice project . In particular, see Information on access to modules . I have not tried this since it was not released, but I was considering copying some code.

As I did in the paste, just let the Guice components be driven by Guice and get them either in Spring Java Config ( @Bean ) using Guice directly, or use the nasty static services locator pattern (i.e. the static method for dynamically extracting the component from Guice).

The biggest problem I encountered, regardless of approach, is figuring out which shoes first, since you have competing life cycles (theoretically this would be a spring-guice project).

0
source

Both Spring and Guice JSR 330 support : Injection Dependency for Java . If the Guice component you need is defined using JSR 330 annotations, or if you have access to the code, you can change the annotations, then you can use it in the Spring bean.

See Spring: Using Standard JSR 330 Annotation and Guice JSR -330 Integration

-1
source

Source: https://habr.com/ru/post/953907/


All Articles