Spring Mvc 自动装配

Spring Mvc 自动装配

  • 版本要求
    • Spring Framework 3.1 +
    • Servlet 3.0 +
  • Servlet SPI
    • Servlet SPI ServletContainerInitializer ,参考 Servlet 3.0 规范
  • 配合 @HandlesTypes
  • Spring 适配
    • SpringServletContainerInitializer
  • Spring SPI
    • 基础接口: WebApplicationInitializer
    • 编程驱动: AbstractDispatcherServletInitializer
    • 注解驱动: AbstractAnnotationConfigDispatcherServletInitializer

示例

  • 导入import org.springframework.web.servlet.DispatcherServlet,添加@ComponentScan注解配置包扫描
import org.springframework.web.servlet.DispatcherServlet;
@ComponentScan(basePackages = "com.imooc.web")
public class DispatcherServletConfiguration {
}

  • 继承抽象类AbstractAnnotationConfigDispatcherServletInitializer
public class DefaultAnnotationConfigDispatcherServletInitializer extends
        AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() { // web.xml
        return new Class[0];
    }
    @Override
    protected Class<?>[] getServletConfigClasses() { // DispatcherServlet
        return new Class[]{DispatcherServletConfiguration.class};
    }
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}
posted @ 2020-06-04 09:03  樊梨花大大王  阅读(246)  评论(0编辑  收藏  举报