문제

XML 기반 구성을 제거하기 위해 봄과 @Configuration 스타일을 정말로 좋아합니다. 나는 서비스 및 저장소 레이어에 성공적으로 사용합니다. 내가 좋아하는 것은 종속성 주입 기능과 JDO / JPA / JDBC 유틸리티입니다!

봄 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/*");
    }

}
.

이제는 Type Converters 및 HttpMessageConverters를 추가하고 싶습니다. SpringWebConfiguration 클래스에서 시도한 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;
    }
}
.

Dispatcher Servlet을 구성 할 때 타입 및 메시지 변환기를 추가하는 것이 중요합니다. 그것은 모호한 자료 또는 빈 창조보다 훨씬 더 명확합니다. 나는 파견자 서블릿이 내 콩을 내부적으로 찍은 "희망"을 항상 "희망"하지만, 그것은 종종 재판과 오류가 종종 있습니다. 에 봄 MVC를 설정할 수 있습니까? 마술이 적고 구체적인 인스턴스화와 #addhttpmessageConverter (...)는 예를 들어 호출합니다.

기본적으로 ExceptionResolvers, RequestHandler 및 RequestAdapter의 경우

1 월

도움이 되었습니까?

해결책

가장 직접적인 방법은 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를 재정의하여 추가 할 수 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top