Springmvc(4.2.4) 注解配置和 xml配置

做下记录

配置对比

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具有同样的作用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值