首页 > 解决方案 > 在 Spring Boot 应用程序中访问基于 google Guice 的服务

问题描述

我有使用 Google guice DI 框架开发的服务,我想将它们注入到 Spring Boot 应用程序中。任何人都可以了解我们如何在 Spring Boot 应用程序中访问基于 google guice 的服务吗?任何指向任何示例代码的指针?

标签: spring-bootguice

解决方案


欢迎来到 StackOverflow!

根据您在 Guice 服务中实际执行的操作,您可以执行两种不同的操作。

  1. 如果你只使用JSR330注解,那么理论上你可以简单地用 Spring@Configuration类替换 Guice 模块,然后只使用 Spring 来执行 DI 这会起作用,因为 Spring 和 Guice 都支持JSR330注解。在我看来,这只能在相当简单的情况下工作。

  2. 使用org.springframework.guice:spring-guiceSpring 中的库。它允许您使用 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 。


推荐阅读