spring 零xml配置 & 内置tomcat 启动spring mvc

xml方式配置spring mvc

  1. 编写web.xml;注册listener、注册servlet、注册servlet-mapping
  2. 编写spring-mvc.xml
  3. 执行servlet规范:
  4. 在"根目录/resources/META-INF/services/"添加配置文件javax.servlet.ServletContainerInitializer
  5. 自定义一个类实现spring提供的WebApplicationInitializer接口,实例化&初始化spring容器
  6. 自定义controller类和业务逻辑
  7. 打包部署到web容器(通常使用tomcat)

零xml配置 & 内置tomcat 启动spring mvc

build.gradle添加依赖
    compile group: 'org.springframework', name: 'spring-webmvc', version: '5.1.12.RELEASE'
    // 内置tomcat需要
    compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-core', version: '8.5.31'
    // 消息转换器需要
    compile group: 'com.alibaba', name: 'fastjson', version: '1.2.47'
resources/META-INF/services/javax.servlet.ServletContainerInitializer
org.springframework.web.SpringServletContainerInitializer
UserEntity
public class UserEntity {
	private String name;
	private String age;
	...// get set constructor省略
}
AppConfig 代替spring-mvc.xml
@Configuration
@ComponentScan("com.tbryant.springmvctest")
// 注解方式驱动springmvc,等同于<annotation:driver>
// 不加不会解析WebMvcConfigurer接口
// 如果不加@EnableWebMvc,就要改成extends WebMvcConfigurationSupport
@EnableWebMvc
public class AppConfig implements WebMvcConfigurer {
	// 配置视图解析器
	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
//		registry.jsp("/page/",".html");
	}
	// 配置消息转换器 这里用fastjson,可以自由选择
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		FastJsonHttpMessageConverter fastJsonHttpMessageConverter=new FastJsonHttpMessageConverter();
		converters.add(fastJsonHttpMessageConverter);
	}
}
MyWebApplicationInitializer 注册servlet和注册servlet-mapping
// 该类从官方文档拷贝,主要目的初始化spring容器,注册servlet和注册servlet-mapping
public class MyWebApplicationInitializer implements WebApplicationInitializer {
	// ServletContext代替web.xml
	@Override
	public void onStartup(ServletContext servletCxt) {
		// 实例化spring容器
		AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
		ac.register(AppConfig.class);
//		ac.refresh();
		// DispatcherServlet注册到web容器
		DispatcherServlet servlet = new DispatcherServlet(ac);// servlet
		ServletRegistration.Dynamic registration = servletCxt.addServlet("DispatcherServlet", servlet);
		registration.setLoadOnStartup(1);
		registration.addMapping("/");// servlet-mapping
	}
}
Application 内置tomcat & 注册listener
public class Application {
    public static void main(String[] args) throws Exception {
        // 内置tomcat
        Tomcat tomcat=new Tomcat();
        tomcat.setPort(8080);
        Context context=tomcat.addContext("/",System.getProperty("java.io.tmpdir"));
        // 注册listener
        context.addLifecycleListener((LifecycleListener)Class.forName(tomcat.getHost().getConfigClass()).newInstance());
        tomcat.start();
        tomcat.getServer().await();
    }
}
UserController
@Controller
public class UserController {
    @RequestMapping("/getuser")
    @ResponseBody
    public UserEntity getUser() {
        return new UserEntity("TBryant", "18");
    }
}
运行main函数 执行结果

在这里插入图片描述

发布了14 篇原创文章 · 获赞 3 · 访问量 869

猜你喜欢

转载自blog.csdn.net/qq_37956177/article/details/104014773