Spring4 @EnableWebMvc 纯注解启动
一、WebConfig
想要以Java形式定制默认的配置,你可以简单的实现WebMvcConfigurer接口,或者继承WebMvcConfigurerAdapter并重写需要的方法
Spring MVC配置核心分发控制器Servlet,即DispatcherServlet,传统做法是直接使用web.xml进行配置.
现在要做的是基于注解配置,需要继承AbstractAnnotationConfigDispatcherServletInitializer。
AbstractAnnotationConfigDispatcherServletInitializer这个类负责配置DispatcherServlet、初始化Spring MVC容器和Spring容器。
加载过程:
1、当DispatcherServlet启动时,会创建一个Spring MVC应用容器并开始加载配置文件中定义好的beans。
通过getServletConfigClasses()方法,可以获取由DispatcherServlet加载的定义在WebConfig.class中的beans,例如:
controllers(控制器)、view resolvers(视图解析器)和处理器映射(handler mappings)
一、WebConfig
想要以Java形式定制默认的配置,你可以简单的实现WebMvcConfigurer接口,或者继承WebMvcConfigurerAdapter并重写需要的方法
@Configuration表示这是Java配置类;@EnableWebMvc注解用于启动Spring MVC特性。
package com.my;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
/**
* MVC 配置类
* @author fly for fun
*
*/
@Configuration
@ComponentScan("com.my")
@EnableWebMvc
public class MyWebConfig extends WebMvcConfigurerAdapter {
/**
* 配置JSP视图解析器
* 如果没有配置视图解析器。Spring会使用BeanNameViewResolver,通过查找ID与逻辑视图名称匹配且实现了View接口的beans
* @return
*/
@Bean
public InternalResourceViewResolver setupViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver ();
/**设置视图路径的前缀*/
resolver.setPrefix("/WEB-INF/");
/**设置视图路径的后缀*/
resolver.setSuffix(".jsp");
return resolver;
}
}
二、WebInitializer
Spring MVC配置核心分发控制器Servlet,即DispatcherServlet,传统做法是直接使用web.xml进行配置.
现在要做的是基于注解配置,需要继承AbstractAnnotationConfigDispatcherServletInitializer。
AbstractAnnotationConfigDispatcherServletInitializer这个类负责配置DispatcherServlet、初始化Spring MVC容器和Spring容器。
加载过程:
1、当DispatcherServlet启动时,会创建一个Spring MVC应用容器并开始加载配置文件中定义好的beans。
通过getServletConfigClasses()方法,可以获取由DispatcherServlet加载的定义在WebConfig.class中的beans,例如:
controllers(控制器)、view resolvers(视图解析器)和处理器映射(handler mappings)
2、还有另一个Spring应用容器,这个容器由ContextLoaderListener创建,加载 @Repository,@Service等
package com.my;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
* Java配置DispatcherServlet
* @author fly for fun
*/
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
/**获取Spring应用容器的配置文件*/
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
/**负责获取Spring MVC应用容器,这里传入预先定义好的MyWebConfig.class*/
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { MyWebConfig.class };
}
/**
* 负责指定需要由DispatcherServlet映射的路径,
* 这里给定的是"/",意思是由DispatcherServlet处理所有向该应用发起的请求
*
* */
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
三、MyController
package com.my;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/***
* 编写简单的控制器
* @author fly for fun
*
*/
@Controller
public class MyWebController {
@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public String home() {
return "welcome";
}
}
四、pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my</groupId>
<artifactId>myWebProject</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>myWebProject Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<warName>myWebProject</warName>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<finalName>myWebProject</finalName>
</build>
</project>
五、JSP
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>HelloWorld page</title>
</head>
<body>
Welcome to myWebProject!
</body>
</html>
六、运行结果