spring-guice icon indicating copy to clipboard operation
spring-guice copied to clipboard

Using Spring Bean in a Class implemented from a Interface from Guice Module

Open anudhij opened this issue 1 year ago • 1 comments

I have a Spring Boot project with Java 17, where I have to add a Maven Dependency which used Guice DI. I cannot change any code in the Library, and I am implementing an Interface from the Dependency and I have done the binding using Guice.

When I try to add any Spring Bean (say S3Helper) using @Autowired in the Implementation class it initialises the instance as Null. The S3Helper class is annotated with Annotation and has it's own constructor with @Autowired Components.

Can you suggest some solution, where my Spring Beans can be made available in the Implementation class.

anudhij avatar Jan 18 '24 09:01 anudhij

We use a modulith design and reuse guice modules with this library. The way we use it is like this in our @SpringBootApplication annotated class; I hope this helps you

    // Spring Guice
    @Configuration
    @EnableGuiceModules
    public static class GuiceClient1ForSpring extends GuiceClient1 {}
    
    // Spring Guice
    @Configuration
    @EnableGuiceModules
    public static class GuiceClient2ForSpring extends GuiceClient2 {}

We just repeat this for other modules to enable it in Spring. We mainly use PrivateModules and expose only what we need

sblommers avatar Feb 01 '24 18:02 sblommers