2014-01-10 32 views
0

我真的很喜歡spring和@Configuration風格來擺脫基於xml的配置。我成功地將它用於服務和存儲庫層。我還喜歡的是依賴注入功能和JDO/JPA/Jdbc實用程序!SpringMVC和@Configuration:註冊ConversionService和HttpMessageConverter

我真的不明白Spring WebMVC是如何工作的。對我來說有太多無法控制的魔法。 (並且使用@EnableAutoConfiguration還有更多的魔法介紹,很容易進行原型設計,難以維護)。

這就是我如何配置我的web應用程序:

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在內部爲我的bean做好準備,但這往往只是試驗和錯誤。 是否可以直接設置彈簧Mvc?用更少的魔法和更具體的實例和#addHttpMessageConverter(...)調用例如?

對於ExceptionResolvers,RequestHandler和RequestAdapter基本相同。

回答

3

最直接的方法是擴展WebMvcConfigurationSupport。您可以通過覆蓋方法來設置幾乎所有的方法。

但請注意,這是一種非常直接的方式來設置的東西。它給你更多的控制權,比你現在或甚至WebMvcConfigurerAdapter會給你。從該文檔:

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 

自定義(或定製的)消息轉換器可以通過重寫configureMessageConverters加入。

+0

這是一個非常好的方法!它爲我創建的所有自定義類型/轉換器/提供了掛鉤。我推薦這種設置spring mvc項目的方式! – Jan

2

如果你的Web配置擴展WebMvcConfigurerAdapter,應該感覺有點不太魔術,也給你的鉤配置信息轉換器以及其他一些組件。

@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); 
     } 

    } 
+0

@zeroflagL感謝您關於'WebMvcConfigurationSupport'的信息,我沒有遇到過那個。它可能證明是有用的。不過,我認爲,有些開發人員認爲他們需要大量的控制和定製,因爲他們真的應該看看他們爲什麼要使用框架,同時要與公約打交道。 (對不起,沒有足夠的_points_評論你的答案。) – englishteeth

+0

也是一個很好的答案。我只是喜歡另一種方法來處理我的具體情況。 – Jan