1
做下记录

配置对比

1、注解配置

从spring官方文档中可以看到如下配置:

public class MyWebApplicationInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletCxt) {

        // Load Spring web application configuration
        AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
        //这里的AppConfig.class实际上是通过人为添加的具体配置类(比如视图解析器、静态资源处理都可以放在这里使用),针对这个类我进行了类似的实现
        ac.register(AppConfig.class);
        //这里是对spring的上下文进行刷新
        ac.refresh();

        // Create and register the DispatcherServlet
        DispatcherServlet servlet = new DispatcherServlet(ac);
        //这里在servlet上下文中添加了一个名为 app 的拦截器(servlet),因为拦截器的实质就是一个servlet 
        ServletRegistration.Dynamic registration = servletCxt.addServlet("app", servlet);
        registration.setLoadOnStartup(1);
        //所有带有/app/ 前缀的请求都交由该拦截器处理
        registration.addMapping("/app/*");
    }
}
 

2、xml配置:

<web-app>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/app-context.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>app</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>app</servlet-name>
        <url-pattern>/app/*</url-pattern>
    </servlet-mapping>    
</web-app>

这里是我自己实现的两个初始化类:

/**
 * @author: Andy
 * @date: 4/2/2018 3:07 PM
 * @description: 通过实现接口配置的初始化类
 * @version: 1.0
 */
public class WebApplicationInitializerConfigA implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(WebConfig.class);
        ctx.setServletContext(servletContext);
        ctx.refresh();
        ServletRegistration.Dynamic registration = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        registration.addMapping("/");
        registration.setLoadOnStartup(1);
    }
}

/**
 * @author: Andy
 * @date: 3/30/2018 5:17 PM
 * @description: 开启视图解析器配置
 * @version: 1.0
 */
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.*"})
public class WebConfig{
    /**
     * Method Description:
     * 〈Jsp 视图解析器〉
     *
     * @param: null
     * @return: jsp view resolver
     * @author: Andy
     * @date: 4/2/2018 9:55 AM
     */
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        //请求前缀,当请求发起时,拦截器会到这里去找相应的页面
        viewResolver.setPrefix("/WEB-INF/views/");
        //请求后缀,控制器返回的内容拼接.jsp寻找页面
        viewResolver.setSuffix(".jsp");
        viewResolver.setExposeContextBeansAsAttributes(true);
        return viewResolver;
    }
}
通过这样简单的配置就已经简单的配置了Springmvc,另外,如果你支持servlet3.0,还可以通过继承一个抽象类来完成配置,如下:
/**
 * @author: Andy
 * @date: 3/30/2018 5:11 PM
 * @description: Web应用初始化配置
 * @version: 1.0
 */
public class WebApplicationInitializerConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    //拦截器将要拦截的请求格式 / 为所有请求
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    //根配置
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[0];
    }    

    //具体配置件类(和上述的WebConfig是一样的)
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebConfig.class};
    }
}

这个类和WebApplicationInitializerConfigA具有同样的作用。


Andy
54 声望11 粉丝

Remember,Hope is a good thing,maybe the best of things and no good thing ever dies!


引用和评论

0 条评论