servlet3.0下構建spring web項目

servlet3.0最顯著的特徵就是增加了servlet、listener和filter等類的註解,不需要將此配置在xml文件中,極大方便了編程。web項目中去掉web.xml配置文件,應該是以後構建web工程的趨勢,在使用myeclipse2014版本中創建web工程,應該可以覺察到,默認構建web項目,web.xml文件已經不存在了。spring3.1之後的版本均支持servlet3.0。
本文中使用myeclipse2014,spring4.0,基於servlet3.0,使用java配置的方式來創建web項目。在spring web項目,會創建兩個上下文,並加載配置文件和聲明的bean,其中一個是由DispatcherServlet創建,它主要加載web組件的bean,如控制器、視圖解析器以及處理器映射,另外一個是由ContextLoaderListener生成的,它主要是加載其它應用的bean,如數據層。因此,在傳統spring web項目中需要在web.xml配置這兩項。基於servlet3.0的spring web項目如何實現呢?
1.首先創建web項目【spittr】,然後創建SpittrWebAppInitializer類:

package spittr.config;
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class SpittrWebAppInitializer extends
        AbstractAnnotationConfigDispatcherServletInitializer {

    //獲取ContextLoaderListener上下文的bean
    @Override
    protected Class<?>[] getRootConfigClasses() {
        // TODO Auto-generated method stub
        return new Class<?>[]{RootConfig.class};
    }

    //獲取DispatcherServlet上下文的bean
    @Override
    protected Class<?>[] getServletConfigClasses() {
        // TODO Auto-generated method stub
        return new Class<?>[]{WebConfig.class};
    }

    //將一個或多個路徑映射到DispatcherServlet,相當於xml中<url-pattern>
    @Override
    protected String[] getServletMappings() {
        // TODO Auto-generated method stub
        return new String[]{"/"};
    }

    @Override
    protected void customizeRegistration(Dynamic registration) {
        // TODO Auto-generated method stub
        //super.customizeRegistration(registration);
        //啓用multipart
        registration.setMultipartConfig(new MultipartConfigElement("C:/temp/",2097152,4194304,0));
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}

SpittrWebAppInitializer類繼承AbstractAnnotationConfigDispatcherServletInitializer類,這個AbstractAnnotationConfigDispatcherServletInitializer類實現了javax.servlet.ServletContainerInitializer接口,在servlet3.0環境中,容器會自動查找實現這個接口的類,並用它來配置servelt上下文。實際上,AbstractAnnotationConfigDispatcherServletInitializer會同時創建DsipathcerServlet和ContextLoaderListener。其中getRootConfigClasses()方法返回的帶有@configuration註解的類將會用來配置ContextLoaderListener創建的應用上下文中的bean。getServletConfigClasses()方法返回的帶有@configuration註解的類將會用來定義DsipatcherServlet應用上下文中的bean。因此AbstractAnnotationConfigDispatcherServletInitializer來配置DsipatcherServlet是傳統web.xml方式的替代方案。
@configuration理解
@configuration註解是用來創建java配置類,帶有這個註解的類表明這個類就是一個配置類,spring上下文會自動掃描配置類中的@bean註解,自動生成bean。
2.啓動spring mvc
首先創建WebConfig類:

package spittr.config;

import java.io.IOException;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
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.WebMvcConfigurationSupport;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan("spittr.web")
public class WebConfig extends WebMvcConfigurationSupport {

    @Bean
    public MultipartResolver multipartResolver()throws IOException{
        return new StandardServletMultipartResolver();
        //return new CommonsMultipartResolver();
    }

    @Bean
    //配置視圖解析器
    public ViewResolver viewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/jsp/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }
    @Override
    //配置對靜態資源的處理,不過濾靜態資源
    protected void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        // TODO Auto-generated method stub
        //super.configureDefaultServletHandling(configurer);
        configurer.enable();
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}

傳統啓用spring mvc是在xml文件中添加<mvc:annotation-driven>,而現在只需要在類上添加@EnableWebMvc註解即可。@ComponentScan會掃描spitter.web包下的類生成bean,spitter.web包下可以是控制層(controller)。
現在,servlet3.0下搭建spring web項目基本框架已經完成,數據層可以在RootConfig下生成。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章