到目前为止,我仅使用Spring引导程序和模型映射器。在Spring引导中,我只需要创建一个Bean,然后在其中配置模型映射器(类似于自定义转换器),然后只返回它的一个实例即可。
但是现在我在一个有关Google Cloud的“普通” Maven项目中,我们使用Cloud Endpoints。对于依赖注入,我们使用的是Guice。并且我们将像这样设置我们的特权:
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 Boot中,我只是这样配置了模型映射器:
@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;
}
所以我的问题是:如何像在Spring引导中一样,用Guice在当前项目中配置Model Mapper。
谢谢!