我有使用Google guice DI框架开发的服务,我想在spring boot应用程序中注入它们。任何人都可以了解我们如何在Spring启动应用程序中访问基于google guice的服务?任何示例代码的指针?
欢迎来到StackOverflow!
根据您在Guice服务中的实际操作,您可以执行两种不同的操作。
JSR330
注释,那么你理论上可以简单地用Spring @Configuration
类替换Guice模块,并且只使用Spring来做D.I.这可行,因为Spring和Guice都支持JSR330
注释。在我看来,这只能在相当简单的情况下起作用。org.springframework.guice:spring-guice
库。它允许您使用Guice模块和提供的bean。首先,您需要将它包含在您的pom中:
<dependency>
<groupId>org.springframework.guice</groupId>
<artifactId>spring-guice</artifactId>
<version>1.1.3.RELEASE</version>
</dependency>
然后,您必须配置Spring以使用所需的Guice模块:
@Configuration
@EnableGuiceModules
public static class GuiceConfig {
@Bean
public MyModule myModule() {
return new MyModule();
}
}
您可以在GitHub上阅读更多关于此项目的信息:https://github.com/spring-projects/spring-guice#using-existing-guice-modules-in-a-spring-applicationcontext。