基於JavaConfig配置的Spring MVC的構建

上一篇講了基於XML配置的構建,這一篇講一講基於JavaConfig的構建。爲何要寫這篇文章,由於基於xml配置的構建,本人認爲很麻煩,要寫一堆的配置,不夠簡潔,而基於JavacConfig配置的構建符合程序員的編碼習慣,是SpringMVC之後的趨勢。不是說基於XML配置的很差,畢竟如今不少已有的項目都是基於XML的,只是基於JavaConfig配置的更優化,並且兩種方式能夠魚和熊掌兼得。但要知道的是,基於JavaConfig配置構建的Spring MVC項目只能部署到支持Servlet 3.0服務器上面。html

進入正題,開始搭建項目:java

搭建環境:程序員

1. IDEAweb

2. Mavenspring

3. Tomcat後端

搭建步驟:api

1. 建立maven-archetype-webapp項目,這個項目是maven的web項目服務器

2.在pom中添加sping mvc相關的依賴包,這個依賴包是我本身使用的,包含了單元測試和相關的mvc mock包:mvc

<dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/javax.servlet.jsp.jstl-api -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-jpa</artifactId>
      <version>1.10.1.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.3.8.RELEASE</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.3.1</version>
    </dependency>

    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>2.8.47</version>
    </dependency>

  </dependencies>

3.將DispatcherSevlet配置在Servlet容器中app

public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    /*加載後端的中間層和數據層組件*/
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{RootConfig.class};
    }

    /*加載web組件,控制器、試圖解析器、處理器映射*/
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

示例代碼中要知道的是,任何擴展了AbstractAnnotationConfigDispatcherServletInitializer的類都會自動配置DispatcherServlet和Spring應用上下文。實際上,AbstractAnnotationConfigDispatcherServletInitializer會同時建立DispatcherServlet和ContextLoaderListener。getServletConfigClasses()方法返回的帶有@Configuration註解的類會用來定義DispatcherServlet應用上下文中的bean。getRootConfigClasses()方法返回的帶有@Configuration註解的類會用來定義ContextLoaderListener應用上下文中的bean。

4.建立WebConfig類

@Configuration
@EnableWebMvc
@ComponentScan("spittr.web")
public class WebConfig extends WebMvcConfigurerAdapter {

    /*配置視圖解析器*/
    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return  resolver;
    }


    /*配置靜態資源的處理*/
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

5.建立RootConfig類

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

}

6,到這裏配置已經結束。怎樣建立Controller和運行請參考上一篇基於XML配置的Spring MVC

7.最後附上源碼源碼

相關文章
相關標籤/搜索