spring-boot - 在 Spring Boot 应用程序中访问基于 google Guice 的服务
问题描述
我有使用 Google guice DI 框架开发的服务,我想将它们注入到 Spring Boot 应用程序中。任何人都可以了解我们如何在 Spring Boot 应用程序中访问基于 google guice 的服务吗?任何指向任何示例代码的指针?
解决方案
欢迎来到 StackOverflow!
根据您在 Guice 服务中实际执行的操作,您可以执行两种不同的操作。
如果你只使用
JSR330
注解,那么理论上你可以简单地用 Spring@Configuration
类替换 Guice 模块,然后只使用 Spring 来执行 DI 这会起作用,因为 Spring 和 Guice 都支持JSR330
注解。在我看来,这只能在相当简单的情况下工作。使用
org.springframework.guice:spring-guice
Spring 中的库。它允许您使用 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 。
推荐阅读
- python - 如何在python3中将我的oneliner小代码变成multiliner?
- flutter - MediaQuery.of() 不断显示
- flutter - 如何使用不同的控制器同步两个或多个 PageView
- r - 在 R 中使用 rvest 从 html 网页中抓取某些数据
- python - 我不明白为什么 np.argwhere 没有为我的第一行返回一些东西
- python - 当我尝试在 python 中设置自己的 VENV 时访问被拒绝,我该怎么办?
- python - PyTorch:使用 1-D 张量和多通道 (3-D) 图像张量执行 add/sub/mul 操作
- unity3d - 使用 MRTK 和 Hololens 重置相机变换
- javascript - 如何使用javascript将地图推送到新数组
- azure - Azure 用户只能分配给组