Spring MVC 零配置 / Spring MVC JavaConfig

1. Spring MVC的核心就是DispatcherServlet类,Spring MVC处理请求的流程以下图所示:

 

 

2. Spring MVC中典型的上下文层次

当咱们初始化一个DispatcherServlet类时,Spring MVC会在web应用的WEB-INF目录下查找一个名字叫:[servlet-name]-servlet.xml的配置文件,查询这个文件中定义的bean并初始化。[servlet-name]-servlet.xml的定义的bean初始化时将会覆盖在全局范围内(global scope)定义的相同名称的bean。咱们通常会在web.xml中定义DispatcherServlet。由上图咱们能够看出Controller、HandlerMapping、ViewResovler类(和web有关的)在Servlet WebApplicationContext中定义,而Services类,Repositories类(中间服务、数据源等)在Root WebApplicationContext中定义。java

3. Spring MVC中单个的根上下文(Single Root Contexct)

 若是只有一个单一个根上下文,这样咱们就能够定义一个空的contextConfigLocation,所以全部的beans就要在这这个单一的Root Context中定义了。web

4. 如何再也不要web.xm和[servlet-name].servlet.xml而采用纯粹的java类来实现?

 

从上图中咱们能够看出只要继承了AbstractAnnotationConfigDispatcherServletInitializer类并实现了相关方法便可实现注册一个DispatcherServlet类。安全

4.1.将 DispatcherServlet 配置在 Servlet 容器中而再也不使用 web.xml 。app

public class RtpFrontWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    /**
     * 指定 Root WebApplicationContext 类,这个类必须@Configuration来注解,从而代替XML配置文件
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{RootConfig.class};
    }

    /**
     * 指定 Servlet WebApplicationContext 类,这个类必须@Configuration来注解,从而代替XML配置文件
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{WebConfig.class};
    }

    /**
     * 指定 Servlet mappings
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

 4.2 定义RootConfig类ide

@Configuration
@ComponentScan(basePackageClasses = Configs.class,
        excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class)})
public class RootConfig {
}

RootConfig类使用了@Configuration做为注解,而@Configuration自己是使用了@Component进行了注解。因此这个RootConfig类是能够被组件扫描到并注入到容器中的。@Configuration用来讲明这个是配置类用来替换原来的xml。了实现组件自动扫描和实例化并注入到容器中,咱们要在配置类中加上@ComponnetScan注解,并设定扫描的包的范围。本示例扫描的是Configs.class类所在的包。函数

 4.3 定义WebConfig类工具

@Configuration
@EnableWebMvc
@ComponentScan(basePackageClasses = HomeController.class)
public class WebConfig {
}

经过@EnableMvc来导入Spring MVC的相关配置,经过@ComponentScan来自动扫描控制器类HomeController所在的包。一样,@Configuration用来讲明这个是配置类用来替换原来的xml。atom

5. 从Servlet容器中(Bean工厂)获取bean.

 下面是一个工具类,代码以下:spa

public class SpringContextUtil {

    /**
     * 私有构造函数,不容许实例化
     */
    private SpringContextUtil(){
    }

    /**
     * Spring应用上下文环境
     */
    private static AnnotationConfigWebApplicationContext ctx;

    static {
        ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(RootConfig.class);
        ctx.refresh();
    }

    /**
     * 获取指定Bean的实例
     *
     * @param name 要获取Bean的名称
     * @return Bean object
     */
    public static Object getBean(String name) {
        return ctx.getBean(name);
    }
}

 定义了一个工具类来从servlet容器中获取bean。这些bean都是被自动扫描并注入到servlet容器中去的。默认状况下,这个bean的实例都是单例的。获取bean的方法以下:code

 AtomTransaction atomTransaction = (AtomTransaction) SpringContextUtil.getBean(getBeanName(transType));

 至此,咱们已经完成了Spring MVC的零配置实现,主要是用相关的Java类来代替以前的XML文件。

 

总结:

一、Spring MVC的核心是DispatcherServlet类,它主要实现请求的路由和相关流程的控制;

二、传统DispatcherServlet类的初始化由读取web.xml和[servlet-name]-servlet.xml的相关配置来实现;

三、为何咱们要采用annotation而非xml?基于java的配置,它比基于XML的配置更增强大、类型安全而且易于重构;

四、零配置,其实很简单:继承AbstractAnnotationConfigDispatcherServletInitializer这个抽象类并实现3个相关的抽象方法,3个抽象方法中分别指定:Servlet WebApplicationContext(WebConfig)、Root WebApplicationContext(RootConfig)、Mappings。WebConfig和RootConfig必需要有@Configuration注解和@ComponentScan注解。@Configuration注解用来代表这些是配置类,用来替换XML;@ComponentScan注解用来实现组件或Bean的自动化扫描及在容器中注册(默认为初始化单例)。

五、使用AnnotationConfigWebApplicationContext来从容器中获取Bean,这些Bean默认都是单例的。

 

帮助到您了吗?

打赏做者(支付宝):

相关文章
相关标签/搜索