Web MVC 注解驱动

Web MVC 注解驱动

版本要求Spring Framework 3.1 +

基本配置步骤

  • 注解配置: @Configuration ( Spring 范式注解 )
  • 组件激活: @EnableWebMvc (Spring 模块装配)
  • 自定义组件 : WebMvcConfigurer (Spring Bean)

原理

在@EnableWebMvc注解中导入了DelegatingWebMvcConfiguration配置类,其父类WebMvcConfigurationSupport配置了许多HandlerMapping和HandlerAdapter以及ViewResolver类

实现注解驱动

  • 开启xml包扫描
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.imooc.web"/>
</beans>
  • 编写配置类,添加@Configuration和@EnableWebMvc注解,配置ViewResolverBean
@Configuration
@EnableWebMvc
public class WebMvcConfig{
    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
}
  • 上面的配置类也可以选择继承WebMvcConfigurer接口,里面提供了默认方法都是空实现,所以里面的方法不不必都实现,一下示例重写添加拦截器的方法添加拦截器
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                System.out.println("拦截中...");
                return true;
            }
        });
    }
}
  • 上述方法添加拦截器底层实现
    @EnableWebMvc注解中导入的DelegatingWebMvcConfiguration中有以下所示成员变量和方法
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
	private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();
	@Autowired(required = false)
	public void setConfigurers(List<WebMvcConfigurer> configurers) {
		if (!CollectionUtils.isEmpty(configurers)) {
			this.configurers.addWebMvcConfigurers(configurers);
		}
	}

它将所有实现WebMvcConfigurer接口的类注入到configurers(WebMvcConfigurerComposite)成员变量中,此变量是个组合对象,WebMvcConfigurerComposite类中如下变量

private final List<WebMvcConfigurer> delegates = new ArrayList<>();

WebMvcConfigurerComposite将调用如下方法注册拦截器

@Override
public void addInterceptors(InterceptorRegistry registry) {
	for (WebMvcConfigurer delegate : this.delegates) {
		delegate.addInterceptors(registry);
	}
}

Web MVC 注解驱动

上一篇:php 上传音频(MP3、MP4等)文件 获取播放时间长度


下一篇:jQuery,Ajax和json