spring-projects / spring-guice

Tools for using Spring in Guice and Guice in Spring
Apache License 2.0
173 stars 65 forks source link

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

Open anudhij opened 8 months ago

anudhij commented 8 months ago

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.

sblommers commented 7 months ago

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