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

API for SpringModule that initiates a spring container itself

Open HomeOfTheWizard opened this issue 1 year ago • 1 comments

I would like to use the project to help using spring based libraries in the development of maven plugins. Maven uses a Guice based DI framework (Sisu), hence to use spring based libraries we need a bridge to inject instances from Spring into Sisu.

One way to do it is to use SpringModule. In Sisu we can automate the creation of custom Guice modules by annotating them with JSR330 @Named. We can then extend the SpringModule, use the constructor to pass an application context that will build the beans we need from our Spring based library, and inject them in Sisu/Guice.

@Named
public class MySpringModule extends SpringModule {

    public MySpringModule(){
        super(new AnnotationConfigApplicationContext(MySpringConfig.class));
    }
}

I think having an API that initiates the container for me would be easier and cleaner. What would be the best is that I can directly give the spring config classes that I need from my libraries, and it does the rest. Something similar to the following

@Named
public class MySpringModule extends SpringModule {

    public MySpringModule(){
        super( [MyLoggingLibraryConfig.class, MyAPICLientLibraryConfig.class, ... ] );
    }
}

Then SpringModule can initiate the container in its constructor and even create a binding to ApplicationContext.class in Guice in case we need it.

If you are interested into this I can gladly create a PR and contribute.

HomeOfTheWizard avatar May 13 '23 16:05 HomeOfTheWizard

Sounds interesting. I'm not sure we want to take another dependency (maybe there isn't one?), but I'd be interested to see the implementation anyway. I think all you are asking might be a convenient way to specify the @Configuration classes instead of passing in an ApplicationContext. I'm not sure I want to add yet another constructor to SpringModule in that case - the first sample above already works, right, so what's wrong with that?

I am even more excited to see an example of a Maven plugin implemented with Spring Guice. Do you have one?

dsyer avatar May 24 '23 05:05 dsyer