【问题标题】:Accessing google Guice based services in spring boot application在 Spring Boot 应用程序中访问基于 google Guice 的服务
【发布时间】:2019-02-28 04:58:58
【问题描述】:

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

【问题讨论】:

标签: spring-boot guice


【解决方案1】:

欢迎来到 StackOverflow!

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

    1234563这会起作用,因为 Spring 和 Guice 都支持 JSR330 注释。在我看来,这只能在相当简单的情况下起作用。
  1. 使用 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。

【讨论】:

  • 我可以在下面的 MyModule 中安装 Guice 外部遗留 guice 模块吗?受保护的静态类 MyModule 扩展 AbstractModule { @Override protected void configure() { install(new LogParserGuiceModule()); } } 并像这样调用 bean:LogParserService sr = context.getBean(LogParserServiceImpl.class);
  • 只要您的模块在 Spring @Configuration 中被实例化为 @Bean,那么您应该能够通过常规 Spring 机制访问该模块中提供的所有服务(通过 @ 注入987654330@,从应用程序上下文中检索,等等)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-25
  • 1970-01-01
  • 1970-01-01
  • 2016-11-28
  • 2015-07-17
  • 1970-01-01
相关资源
最近更新 更多