一、摘要
最为一个名初级Java程序员,学习一些新技术是必不可少,并且在学习的过程中也会收获一些东西,今天我就有了一些意想不到的收获。以前学习SpringMVC和Spring的时候,就是基于SSM或者SSH开发项目时,我们都会使用到XML的方式配置Spring和SpringMVC,这种配置方式首先对于实际开发过程中比较繁琐,而且也不容易维护,所以接下来我们来接触下SpringMVC和Spring整合Servlet3.0实现注解开发。
二、Servlet3.0
Servlet是什么就不用说了,学习JavaEE必须要了解的,我们这里说下Servlet3.0的一些新特性,可在下面这个网站查看
三、简单配置并启动
- 创建Maven的Web项目
- 导入依赖包,请注意Servlet的版本
<!-- SpringMVC 依赖包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
<!-- Servlet3.0 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
- 导入Tomcat插件
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
- 在项目下创建SpringMVC存放配置类的目录
- 编写配置类
在config目录中创建Application配置类,并添加注解
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
/**
* 这个类相当于application.xml配置文件
*/
@Configuration //指定该类为配置配置类
@ComponentScan(basePackages = "init.luoyu.security.springmvc" //指定需要扫描的包
//指定不扫描Contorller注解类
,excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = {Controller.class})})
public class ApplicationConfig{
/**
* 在这下面还需要配置数据源、连接池等信息
*/
}
- 在config目录下创建WebMvcConfig类并实现接口WebMvcConfigurer
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
/**
* 该类相当于springmvc.xml这个配置文件
*/
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "init.luoyu.security.springmvc"
,includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = {Controller.class})})
public class WebMvcConfig implements WebMvcConfigurer {
//定义视图解析器
@Bean
public InternalResourceViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/"); //指定视图路径
resolver.setSuffix(".jsp"); //指定视图后缀
return resolver;
}
//配置视图Controller
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}
}
- 在init目录下创建Spring的启动类,并继承AbstractAnnotationConfigDispatcherServletInitializer 类并重写抽象方法
import init.luoyu.security.springmvc.config.ApplicationConfig;
import init.luoyu.security.springmvc.config.WebMvcConfig;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
* 此类实现WebApplicationInitalizer接口 Spring容器启动时加载该接口下的所有实现类
*/
public class SpringApplicationInitalizer extends AbstractAnnotationConfigDispatcherServletInitializer {
/**
* Spring容器
* @return
*/
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{ApplicationConfig.class}; //指定Spring配置类
}
/**
* ServletContext
* @return
*/
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{WebMvcConfig.class}; //指定SprigMvc配置类
}
/**
* url-mapping
* @return
*/
@Override
protected String[] getServletMappings() {
return new String[]{"/"}; //指定根路径
}
}
-
最后我们使用Maven中指定的tomcat7插件启动应用
1、配置插件
-
最后直接运行
-
启动成功