我真的很喜欢春天和@configuration的风格,以摆脱基于XML的配置。我成功使用它为服务和存储库层。我也喜欢的是依赖注入功能和JDO / JPA / JDBC实用程序!

我真的不是什么,Spring webmvc如何工作。对我来说有太多无法控制的魔力。 (以及@EnableAutoConfiguration介绍了更多的魔法。对于易于原型,难以维持良好)。

是我如何配置我的webapp:

public class SpringWebBooter implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigApplicationContext rootContext = new AnnotationConfigApplicationContext();
        rootContext.register(SpringConfiguration.class); //main configuration class for all beans
        rootContext.refresh();

        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.setParent(rootContext);
        ctx.register(SpringWebConfiguration.class); //web context configuration class

        ServletRegistration.Dynamic api = servletContext.addServlet("api", new DispatcherServlet(ctx));
        api.setLoadOnStartup(1);
        api.addMapping("/api/*");
    }

}
. 现在我想添加类型转换器和httpmessageConverters,所以在SpringWebConfiguration类中我尝试了:

@EnableWebMvc
@Configuration
@ComponentScan
public class SpringWebConfiguration {

    //works but feels very *magic*
    @Autowired
    public void configureConversionService(FormattingConversionService conversionService) {
        conversionService.addConverter(new PointConverter(GEOMETRY_FACTORY));
        conversionService.addConverterFactory(new StringToEnumConverterFactory());
    }

    //not working yet
    @Bean
    public MappingJackson2HttpMessageConverter createJsonMessageConverter() {
        ObjectMapper o = new ObjectMapper();
        o.enable(SerializationFeature.INDENT_OUTPUT);
        MappingJackson2HttpMessageConverter c = new MappingJackson2HttpMessageConverter();
        c.setObjectMapper(o);
        return c;
    }
}
.

我宁愿发现直观是在构建调度程序servlet时添加类型和消息转换器。这比一些可疑的自动控制或豆类创作更清晰。我总是“希望”Dispatcher Servlet在内部拍摄我的豆类,但通常只是试验和错误。 是否可以以导致的方式设置Spring MVC ?具有较少的魔法和更具体的实例化和#addhttpmessageConverter(...)例如?

基本上用于ExceptionResolvers,RequestHandler和RequestAdapter。

Jan

有帮助吗?

解决方案

最直接的方式是扩展生成的。您可以通过覆盖方法来设置几乎所有的一切。

但要意识到它是一个非常直接的方法来设置东西。它会给你比现在的控制更多,也比世纪透明度agcode甚至给你。来自文档:

If the customization options of {@link WebMvcConfigurer} do not expose
something you need to configure, consider removing the {@code @EnableWebMvc}
annotation and extending directly from {@link WebMvcConfigurationSupport}
overriding selected {@code @Bean} methods
.

自定义(或自定义)可以通过覆盖WebMvcConfigurationSupport来添加消息转换器。

其他提示

如果扩展 webmvcconfigureradapter 对于您的Web配置,它应该感觉一点魔法,也可以为您提供钩子配置邮件转换器以及许多其他组件。

    @Configuration
    @ComponentScan
    @EnableWebMvc
    public class WebConfiguration extends WebMvcConfigurerAdapter
    {
      @Autowired
      private CustomObjectMapper domainMapper;

      @Override
      public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
      {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(domainMapper);
        converters.add(converter);
        super.configureMessageConverters(converters);
      }

    }
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top