将Guice组件集成到Spring应用程序中 [英] Integrate Guice component into Spring application

查看:568
本文介绍了将Guice组件集成到Spring应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个基于Spring框架的应用程序,需要集成使用Google Guice构建的组件.

We have a Spring framework based application and need to integrate a component that is built using Google Guice.

有人可以给我们一些建议吗?

Can anybody give us some advice on how this can be done?

我们遇到了以下链接,这些链接显示了如何将Spring集成到Guice中,但反之则需要它:

We came across the following links that show how integrate Spring into Guice, but we need it the other way around:

http://google-guice.googlecode.com/git/javadoc/com/google/inject/spring/SpringIntegration.html

http://www.jroller.com/mindcrime/entry/an_example_of_integrating_guice

感谢您的帮助

推荐答案

无需特殊框架或依赖项即可将guice组件集成到spring-boot.

No Special framework or dependency is required to integrate guice component into spring-boot.

标准启动应用

    @SpringBootApplication
    public class App {
       SpringApplication.run(App.class, appArgs);
    }

指导模块配置

  @Configuration
  public class GuiceBeanConfig {

    private final Injector injector;

    // guice modules are initialized before the spring context completes 
    {
        injector = Guice.createInjector(
                new MyModuleA(),
                new MyModuleB()
        );
    }

    /**
     * Option1: can expose injector as a Spring Bean.
     */
    @Bean
    public Injector injector() {
        return injector;
    }


    /**
     * Option2: expose specific component as a Spring Bean.
     */
    @Bean
    public MyServiceA serviceA() {
        return injector.getInstance(ServiceA.class);
    }
}

作为常规spring bean自动连接到服务组件中.

  • 选项1:自动连接guice注入器并从中访问任何

  • Option1: autowire guice injector and access any bean from it

 @Service
 public class MySpringService {

    private final ServiceA serviceA;  

    MySpringService (Injector i){
       serviceA = injector.getInstance(ServiceA.class)
    }

    public void callServiceA() {
        serviceA.doSomething();
    }

  }

  • 选项2:自动连线特定的豆子

  • Option2: autowire specific bean

    @Service
    public class MySpringService {
    
      private final ServiceA serviceA; 
    
      MySpringService (ServiceA s){
          serviceA = s;
      }
    
      public void callServiceA() {
          serviceA.doSomething();
      }
    
    }
    

  • 注意1: 默认情况下,Spring使用范围: Singleton 和guice: Prototype . 如果您的guice组件未注释为 @Singleton :
    每次创建MySpringService的新实例时, Option1 都会创建ServiceA的实例.
    相反, Option2 将ServiceA公开为具有作用域Singleton的bean.

    NOTE 1: By default Spring uses scope: Singleton and guice: Prototype. In case your guice component is not annotated as @Singleton :
    Option1 creates an instance of ServiceA each time you create a new instance of MySpringService.
    Option2 instead exposes ServiceA as a bean with scope Singleton.

    Singleton与原型:如果组件/服务是线程安全的,则不保持状态,因此更好的选择是Singleton.更好的性能,更少的GC工作.

    Singleton vs Prototype: if you component/service is threadsafe, does not keep state the better option would be Singleton. Much better performance, less work for GC.

    注意2: 如果进行构造函数注入,Spring-boot不需要使用@Autowire注释.

    NOTE 2: Spring-boot does not require to use @Autowire annotation in case you do constructor injection.

    这篇关于将Guice组件集成到Spring应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆