Java 配置SpringMVC

配置DispatcherServlet

DispatcherServlet是SpringMVC的核心。

按照传统的方式,像DispatcherServlet这样的Servlet会配置在web.xml文件中。但是借助于Servlet3规范和Spring3.1功能的增强,这种方式已经不是唯一的方案。并且我个人也极度不喜欢xml的方式。(例如需要引入一堆的命名空间啊以及schemaLocation以及一堆的xml标签)

WebAppInitializer.java

package config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 * 按照AbstractAnnotationConfigDispatcherServletInitializer的定义
 * 它会同时创建DispatcherServlet和ContextLoaderListener
 * 可用此种方式代替传统的web.xml
 * @author yd
 *
 */

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { RootConfig.class };      //指定根配置类,例如:application.xml
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { WebConfig.class };        //ָ指定Web配置类,例如:springMVC.xml
    }

    @Override
    protected String[] getServletMappings() {    //将DispatcherServlet映射到"/"
        return new String[] { "/" };
    }

}

启用SpringMVC

WebConfig.java

package 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;



@Configuration
@EnableWebMvc                                //启动SpringMVC
@ComponentScan("扫描的包名")            //启动组件扫描
public class WebConfig extends WebMvcConfigurerAdapter {

    //配置JSP视图解析器
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }
    
    //配置静态资源的处理
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();        //对静态资源的请求转发到容器缺省的servlet,而不使用DispatcherServlet
    }
    
}

RootConfig.java

package config;

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

@Configuration
@ComponentScan( basePackages={"test"}, 
                excludeFilters = { @Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)}
             //excludeFilters.指定不适合组件扫描的类型。
              )

public class RootConfig {

}

注意:

如果按照此种方式配置DispatcherServlet,而不是使用web.xml的话,那唯一的问题在于它只能部署到支持Servlet3.0的服务器才能工作,如tomcat7或以上版本。

至此最基本的配置已经配好了。但是,如果你想注册其他的Servlet,Filter或Listener的话,那怎么办呢?

添加其他的Servlet,Filter或Listener

想往Web容器中注册其他组件的话,只需创建一个新的初始化器就可以了。最简单的方式就是实现Spring的WebApplicationInitializer接口。

添加Servlet

package config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;

import servlet.MyServlet;

public class MyServletInitializer implements WebApplicationInitializer{

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {

        /** 注册Servlet */
        ServletRegistration.Dynamic myServlet = servletContext.addServlet("myServlet", MyServlet.class); // 注册Servlet
        myServlet.addMapping("/myServlet"); // 映射Servlet

    }

添加Filter

package config;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;

import servlet.MyServlet;

public class MyServletInitializer implements WebApplicationInitializer{

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {

        /** 注册Servlet */
        ServletRegistration.Dynamic myServlet = servletContext.addServlet("myServlet", MyServlet.class); // 注册Servlet
        myServlet.addMapping("/myServlet"); // 映射Servlet

        /** 注册Filter */
        FilterRegistration.Dynamic myFilter = servletContext.addFilter("myFilter", MyFilter.class); // 注册Filter
        myFilter.addMappingForUrlPatterns(null, false, "/myFilter"); // 添加Filter的映射路径

    }

}
最终配置文件目录

以上只是学习Spring实战所写的笔记,如有错误,请指正。谢谢


猜你喜欢

转载自blog.csdn.net/z1790424577/article/details/80719516