基于java初始化springmvc配置

 

通常情况下,我们都是采用web.xml来配置springmvc的,如下所示:

  <servlet>
        <servlet-name>springmvcservlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>/WEB-INF/springmvc.xml</param-value>
        </init-param>
        <init-param>
             <param-name>appName</param-name>
             <param-value>authplatform</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvcservlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

 

在本文中,主要讲的是利用java配置的方法,即不会修改web.xml(需要支持servlet容器3.0及以上,例如tomcat7.0及以上):

写一个实现类,实现了WebApplicationInitializer接口或者继承AbstractAnnotationConfigDispatcherServletInitializer 区别主要在于后者帮我们默认初始化了很多东西。

package com.qcsy.test;

import com.qcsy.test.config.RootConfig;
import com.qcsy.test.config.WebConfig;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 * Description: 使用java 配置DispatcherServlet
 * Copyright: © 2020 qcsystudio. All rights reserved.
 *
 * @author qcsy
 * @version 1.0
 * @timestamp 2020/10/25.
 */
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {

    /**
     * 可以用来配置整个spring容器的相关信息,例如扫描bean的位置等等
     * 返回的class需要待有 @Configuration 或者@Component注解
     * @return
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{RootConfig.class};
    }
    /**
     * 可以用来配置整个dispatcher相关的信息
     * 返回的class需要待有 @Configuration 或者@Component注解
     * @return
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebConfig.class};
    }

    /**
     * 配置映射路径
     * @return
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

getServletMappings:,它会将一个或多个路径映 射到DispatcherServlet

getServletConfigClasses:配置springmvc相关的配置类,如配置视图解析器等等

getRootConfigClasses:配置一个或者多个spring相关容器的配置类

RootConfig代码:

package com.qcsy.test.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * Description:
 * Copyright: © 2019 qcsystudio. All rights reserved.
 *
 * @author qcsy
 * @version 1.0
 * @timestamp 2020/10/25.
 */
@Configuration
@EnableWebMvc
@ComponentScan("com.qcsy.test")
public class WebConfig extends WebMvcConfigurerAdapter {
    /**
     * jsp视图解析器
     * @return
     */
    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver internalResourceViewResolver=new InternalResourceViewResolver();
        internalResourceViewResolver.setPrefix("/");
        internalResourceViewResolver.setSuffix(".jsp");
        internalResourceViewResolver.setExposeContextBeansAsAttributes(true);
        return internalResourceViewResolver;
    }

    /**
     * 配置静态资源处理器
     * 通过 调用DefaultServlet-HandlerConfigurer的enable()方法
     * 我们要求DispatcherServlet将对静态资源的请求转发到Servlet容 器中默认的Servlet上
     * @param configurer
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
       configurer.enable();
    }
}

RootConfig 代码:

package com.qcsy.test.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

/**
 * Description:
 * Copyright: © 2019 qcsystudio. All rights reserved.
 *
 * @author qcsy
 * @version 1.0
 * @timestamp 2020/10/25.
 */
@Configuration
//避免扫描到了webconfig
@ComponentScan(basePackages = "com.qcsy.test",excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = EnableWebMvc.class)})
public class RootConfig {
}

 

到这里通过3个java类就简单实现了springmvc的初始化配置

----------------------------------------------------------------------------------------------------------------------------

下面是这样的实现原理:

在Servlet 3.0环境 中,容器会在类路径中查找实现 javax.servlet.ServletContainerInitializer接口的类, 如果能发现的话,就会用它来配置Servlet容器。

Spring提供了这个接口的实现,名 为SpringServletContainerInitializer,这个类反过来又会 查找实现WebApplicationInitializer的类并将配置的任务交给 它们来完成:如图

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值