如何仅使用 Google Cloud Endpoints 配置 Model Mapper 一次?

How to configure Model Mapper with Google Cloud Endpoints only once?

到目前为止,我只使用过 Spring 引导和模型映射器。在 Spring 引导中,我只需要创建一个 Bean 一次,我将在其中配置模型映射器(诸如自定义转换器之类的东西),然后 return 它的一个实例。

但现在我在一个 "normal" Maven 项目中,该项目是关于 Google Cloud 的,我们使用 Cloud Endpoints。对于依赖注入,我们使用 Guice。我们将 depedencies 设置为像这样注入:

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;

public class GuiceListener extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new GuiceModule());
    }
}



import com.google.api.server.spi.guice.EndpointsModule;
import com.google.common.collect.ImmutableList;

public class GuiceModule extends EndpointsModule {
    @Override
    public void configureServlets() {
        super.configureServlets();
        bind(ModelMapper.class).toInstance(new ModelMapper());
        bind(UsuariosEndpoint.class).toInstance(new UsuariosEndpoint());
        bind(ServiciosEndpoint.class).toInstance(new ServiciosEndpoint());
        configureEndpoints("/_ah/api/*", ImmutableList.of(UsuariosEndpoint.class, 
                ServiciosEndpoint.class));
        bind(ComunidadesAutonomasService.class).to(ComunidadesAutonomasServiceImpl.class);
        bind(CategoriasService.class).to(CategoriasServiceImpl.class);
    }
}

在 Spring 引导中,我只是像这样配置模型映射器:

@Bean("ModelMapper")
    public ModelMapper modelMapper() {
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.getConfiguration().setCollectionsMergeEnabled(false);
        modelMapper.addConverter(new LocalDateToString());
        modelMapper.addConverter(new StringToLocalDate());
        modelMapper.addConverter(new LocalDateTimeToString());
        modelMapper.addConverter(new StringToLocalDateTime());
        return modelMapper;
    }

所以我的问题是:如何使用 Guice 在我当前的项目中配置 Model Mapper,就像我在 Spring 启动时所做的那样。

谢谢!

您可以使用 Google Guice 中的 Provides Methods 来做到这一点。

例子-

public class GuiceModule extends EndpointsModule {
    @Override
    public void configureServlets() {
        super.configureServlets();
        bind(UsuariosEndpoint.class).toInstance(new UsuariosEndpoint());
        bind(ServiciosEndpoint.class).toInstance(new ServiciosEndpoint());
        configureEndpoints("/_ah/api/*", ImmutableList.of(UsuariosEndpoint.class, 
                ServiciosEndpoint.class));
        bind(ComunidadesAutonomasService.class).to(ComunidadesAutonomasServiceImpl.class);
        bind(CategoriasService.class).to(CategoriasServiceImpl.class);
    }

    @Provides
    @Singleton
    public ModelMapper provideModelMapper() {
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.getConfiguration().setCollectionsMergeEnabled(false);
        modelMapper.addConverter(new LocalDateToString());
        modelMapper.addConverter(new StringToLocalDate());
        modelMapper.addConverter(new LocalDateTimeToString());
        modelMapper.addConverter(new StringToLocalDateTime());

        return modelMapper;
    }
}