Working ability is greater than attitude.
talk is cheap, show me the code,make a better result.
目录
概述
作为SpringBoot 家族的一员,SpringBoot是非常重要的。
需求:
需求整理如下:
1.约定由于配置
2.整合jar,自动配置
设计思路
暂无
实现思路分析
1. tomcat
首先引入了Tomcat依赖,然后用java代码启动Tomcat容器,默认Tomcat版本是8.5版本
2.与Spring集成
Tomcat是实现了servlet3.0规范,在servlet加载类的过程中会寻找实现了ServletContainerInitializer接口的类,spring为我们实现了这个接口的类是SpringServletContainerInitializer,最后又会去调用实现了WebApplicationInitializer接口的类,最后真正是实现WebApplicationInitializer接口的是这个AbstractAnnotationConfigDispatcherServletInitializer,然后在该类中去初始化spring容器和springmvc容器和servlet拦截的url
拓展Demo实现
相关代码如下:
1.启动tomcat
public static void start() throws ServletException, LifecycleException {
//创建Tomcat容器和设置端口
Tomcat tomcatServer = new Tomcat();
tomcatServer.setPort(9090);
StandardContext ctx = (StandardContext) tomcatServer.addWebapp("/", new File("src/main").getAbsolutePath());
ctx.setReloadable(false);
WebResourceRoot resources = new StandardRoot(ctx);
File additionWebInfClasses = new File("target/classes");
//告诉Tomcat项目的class目录,根目录
resources.addPreResources(
new DirResourceSet(resources, "/WEB-INF/classes", additionWebInfClasses.getAbsolutePath(), "/"));
//启动容器
tomcatServer.start();
tomcatServer.getServer().await();
}
/**
* @Description:SpringMVC注解配置,视图解析器
*
* @mail:hutao_2017@aliyun.com
* @date 2020年8月11日
*/
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.hutao.springboot.write.controller" })
public class SpringMvcConfig extends WebMvcConfigurationSupport {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/page/");
viewResolver.setSuffix(".jsp");
viewResolver.setExposeContextBeansAsAttributes(true);
return viewResolver;
}
}
实验效果:
分析:
待补充
小结:
主要讲述了SpringBoot原理和简单实现,里面有许多不足,请大家指正~
参考资料和推荐阅读
欢迎阅读,各位老铁,如果对你有帮助,点个赞加个关注呗!~