【发布时间】:2019-02-28 04:58:58
【问题描述】:
我有使用 Google guice DI 框架开发的服务,我想将它们注入到 Spring Boot 应用程序中。任何人都可以了解我们如何在 Spring Boot 应用程序中访问基于 google guice 的服务吗?任何指向任何示例代码的指针?
【问题讨论】:
-
我已经有旧的 guice 服务,并且]hose 服务不能在春季复制。
标签: spring-boot guice
我有使用 Google guice DI 框架开发的服务,我想将它们注入到 Spring Boot 应用程序中。任何人都可以了解我们如何在 Spring Boot 应用程序中访问基于 google guice 的服务吗?任何指向任何示例代码的指针?
【问题讨论】:
标签: spring-boot guice
欢迎来到 StackOverflow!
根据您在 Guice 服务中实际执行的操作,您可以执行两种不同的操作。
JSR330 注释。在我看来,这只能在相当简单的情况下起作用。
使用 Spring 中的 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。
【讨论】:
@Configuration 中被实例化为 @Bean,那么您应该能够通过常规 Spring 机制访问该模块中提供的所有服务(通过 @ 注入987654330@,从应用程序上下文中检索,等等)。