@Configuration和@Bean注释是否适用于此代码?

时间:2017-12-29 04:03:37

标签: java spring spring-mvc

我在一家互联网公司工作。最近我在SpringMVC的配置文件中遇到了问题。

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    public WebConfig() {
    }
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new ByteArrayHttpMessageConverter());
        ClassLoader classLoader = this.getClass().getClassLoader();
        if (ClassUtils.isPresent("javax.xml.bind.Binder", classLoader)) {
            converters.add(new Jaxb2RootElementHttpMessageConverter());
        }
        if (ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", classLoader)) {
            converters.add(new MappingJackson2HttpMessageConverter());
        }
        StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
        stringConverter.setWriteAcceptCharset(false);
        converters.add(stringConverter);
        converters.add(new ResourceHttpMessageConverter());
        converters.add(new SourceHttpMessageConverter());
        converters.add(new XmlAwareFormHttpMessageConverter());
    }
    @Bean
    public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
        RequestMappingHandlerAdapter adapter = super.requestMappingHandlerAdapter();
        List<HandlerMethodReturnValueHandler> handlers = new ArrayList();
        handlers.add(new ModelAndViewMethodReturnValueHandler());
        handlers.add(new ModelMethodProcessor());
        handlers.add(new ViewMethodReturnValueHandler());
        handlers.add(new HttpEntityMethodProcessor(this.getMessageConverters()));
        handlers.add(new ModelAttributeMethodProcessor(false));
        handlers.add(new NullableRequestResponseBodyMethodProcessor(this.getMessageConverters()));
        handlers.add(new ViewNameMethodReturnValueHandler());
        handlers.add(new MapMethodProcessor());
        if (!CollectionUtils.isEmpty(adapter.getModelAndViewResolvers())) {
            handlers.add(new ModelAndViewResolverMethodReturnValueHandler(adapter.getModelAndViewResolvers()));
        } else {
            handlers.add(new ModelAttributeMethodProcessor(true));
        }
        adapter.setReturnValueHandlers(handlers);
        return adapter;
    }
}  

这个类来自外部jar包。

在我的mvc-core-config.xml中有一行:

<bean class="com.wosai.upay.common.config.WebConfig"/>

我想知道这些春季注释是否适用于本课程?

1 个答案:

答案 0 :(得分:1)

据我所知,你的意思是@Configuration,如果是这样,这个类在两种情况下将是一个配置类 1)您将在Dispatcher Servlet中将该传递添加到此类 2)在主配置文件(使用@EnableWebMvc进行了分配的类)中,您将使用@Import导入此类,或者如果您放置@ComponentScan,则此类将自动扫描