在Spring启动应用程序中访问基于Google Guice的服务

问题描述 投票:0回答:1

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

spring-boot guice
1个回答
0
投票

欢迎来到StackOverflow!

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

  1. 如果你只使用JSR330注释,那么你理论上可以简单地用Spring @Configuration类替换Guice模块,并且只使用Spring来做D.I.这可行,因为Spring和Guice都支持JSR330注释。在我看来,这只能在相当简单的情况下起作用。
  2. 使用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

© www.soinside.com 2019 - 2024. All rights reserved.