注解式SpringWebMvc

基本使用

注解式入口(SPI)


public class MyStarterInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    /**
     * 提供父容器的配置类
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{RootConfig.class};
    }

    /**
     * 提供子容器的配置类
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebConfig.class};
    }

    /**
     * 设置DispatcherServlet的映射
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

父容器配置

@Configuration
@ComponentScan(
        basePackages = "org.example.springmvc.annotation",
        excludeFilters ={
                @ComponentScan.Filter(type = FilterType.ANNOTATION,value = {RestController.class, Controller.class}),
                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value = WebConfig.class)
        }
)
public class RootConfig {
}

子容器配置(按需配置)

@Configuration
@ComponentScan(
        basePackages = "org.example.springmvc.annotation",
        includeFilters = {
                @ComponentScan.Filter(type = FilterType.ANNOTATION, value = {RestController.class, Controller.class})
        }
)
@EnableWebMvc // <mvc:annotation‐driven/>
public class WebConfig implements WebMvcConfigurer {
    @Bean
    public MyIntercepter myIntercepter() {
        return new MyIntercepter();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myIntercepter())
                .addPathPatterns("/**");
    }


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


    @Bean
    public AcceptHeaderLocaleResolver localeResolver() {
        AcceptHeaderLocaleResolver acceptHeaderLocaleResolver = new AcceptHeaderLocaleResolver();
        return acceptHeaderLocaleResolver;
    }
}

原理浅析

Tomcat中

ServletContext
    --->startInternal()    启动
        --->onStartup(entry.getValue(),getServletContext())  
                (搜集并触发ServletContainerInitializer的onStartup方法)

SpringMVC中
    
SPI实现类:org.springframework.web.SpringServletContainerInitializer

    @HandlesTypes(WebApplicationInitializer.class)
    public class SpringServletContainerInitializer implements ServletContainerInitializer                 
    {
        获取所有WebApplicationInitializer的非抽象子类,实例化,并onStartup()
    }


关键类:
AbstractContextLoaderInitializer(父容器相关)
AbstractDispatcherServletInitializer(子容器相关)
AbstractAnnotationConfigDispatcherServletInitializer(对上述两类提供支持)

类图关系

AbstractContextLoaderInitializer

public void onStartup(ServletContext servletContext) throws ServletException {
		registerContextLoaderListener(servletContext);
}


protected void registerContextLoaderListener(ServletContext servletContext) {
        // 创建父容器
		WebApplicationContext rootAppContext = createRootApplicationContext();
		if (rootAppContext != null) {

            // 创建ContextLoaderListener并添加等价于
                <listener>
                    <listener-class>
                        org.springframework.web.context.ContextLoaderListener
                    </listener-class>
                </listener>
            //

			ContextLoaderListener listener = new ContextLoaderListener(rootAppContext);
			listener.setContextInitializers(getRootApplicationContextInitializers());
			servletContext.addListener(listener);
		}
		else {
			logger.debug("No ContextLoaderListener registered, as " +
					"createRootApplicationContext() did not return an application context");
		}
}
AbstractDispatcherServletInitializer
AbstractDispatcherServletInitializer

    public void onStartup(ServletContext servletContext) throws ServletException {
        
        //调用AbstractContextLoaderInitializer的onStartup
        super.onStartup(servletContext);
        // 子容器的创建
        this.registerDispatcherServlet(servletContext);
    }


/**

等价于
    <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <!--初始化参数
                contextConfigLocation  配置springmvc的xml配置文件, 指定路径
                也可以不配置: 会自动去WEB-INF去找一个名字叫做  springmvc-servlet.xml 的文件
                -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>

        <!--启动时加载servlet  :
        当web服务器 启动时就会创建servlet(会自动调用servlet的构造函数及init()方法)
        -->
        <load-on-startup>1</load-on-startup>
    </servlet>
*/
protected void registerDispatcherServlet(ServletContext servletContext) {
     //创建子容器
     WebApplicationContext servletAppContext = this.createServletApplicationContext();
     
     //实例化DispatcherServlet
     FrameworkServlet dispatcherServlet=this.createDispatcherServlet(servletAppContext);
     
    dispatcherServlet.setContextInitializers(getServletApplicationContextInitializers());

        Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet);

        // <load-on-startup>1</load-on-startup>
        registration.setLoadOnStartup(1);

        registration.addMapping(this.getServletMappings());
        registration.setAsyncSupported(this.isAsyncSupported());


            Filter[] filters = this.getServletFilters();
            if (!ObjectUtils.isEmpty(filters)) {
                Filter[] var7 = filters;
                int var8 = filters.length;

                for(int var9 = 0; var9 < var8; ++var9) {
                    Filter filter = var7[var9];
                    this.registerServletFilter(servletContext, filter);
                }
            }

            this.customizeRegistration(registration);

}

 上述关键代码,实例化子父容器、Servlet、监听器。

        listenerStart()------启动父容器

        loadOnStartup(findChildren()----启动子容器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值