错过了樱落晚霞
就笑着迎接旭日东升
在传统的spring项目中,web.xml,applicationContext.xml,springmvc.xml在大多数的项目开发中大同小异是缺少不了的
例如
提取web.xml中一些核心的配置文件
<servlet>
<servlet-name>Graduation_Project_1</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml,classpath:spring-redis.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Graduation_Project_1</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
提取springmvc.xml中一些核心的配置文件
<!--配置注解扫描-->
<context:component-scan base-package="cn.test"/>
这些核心配置文件包含着前端控制器等控制整个spring项目核心运行的关键配置,
那么在springboot中是为什么把xml这种核心文件抛弃了呢
看一下spring官网中关于mvc前端控制器的内容
可见,这些核心配置文件内容可通过实现WebApplicationInitializer接口的onStartup方法进行实现,我们尝试一下
public class MyWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
System.out.println("-------------初始化spring环境----------------");
//初始化spring环境
AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
//把配置类注册到spring环境
ac.register(WebConfig.class);
//解析spring环境中的配置类
ac.refresh();
DispatcherServlet dispatcherServlet = new DispatcherServlet(ac);
//把一个servlet注册给容器(tomcat)
ServletRegistration.Dynamic registration = servletContext.addServlet("testServlet", dispatcherServlet);
//设置DispatcherServlet的init在web容器启动时执行init方法
registration.setLoadOnStartup(1);
//配置DispatcherServlet拦截所有请求
registration.addMapping("/");
}
}
配置类
配置类,可当做applicationContent.xml/springmvc.xml配置文件
spring提供
//扫描包下所有的注解
@ComponentScan("cn.test")
//声明这是一个配置类
@Configuration
public class WebConfig {
}
现在xml中的基本的配置有了,spring环境也有了,缺什么?缺容器
现在需要模拟一个Tomcat
模拟tomcat,怎么模拟,看一下SpringBoot源码,了解springboot的内嵌tomcat哪来的
可以看到springboot的内嵌tomcat是来自引用的jar包
tomcat-embed-core-9.0.14.jar
我们也引入这个jar包
引入tomcat
public class Demo {
public static void main(String[] args) {
//操作系统的临时目录
File file=new File(System.getProperty("java.io.tmpdir"));
//启动内嵌Tomcat
Tomcat tomcat = new Tomcat();
//设置端口号
tomcat.setPort(8888);
//指定tomcat工作目录,以/来访问,file.getAbsolutePath()获取当前的绝对路径
tomcat.addContext("/",file.getAbsolutePath());
try {
//启动tomcat
tomcat.start();
//让tomcat阻塞,等待别人连接
tomcat.getServer().await();
} catch (Exception e) {
e.printStackTrace();
}
}
}
执行main方法
tomcat启动了,但为什么没有进入到MyWebApplicationInitializer方法去初始化spring环境?
tomcat怎么知道MyWebApplicationInitializer这个方法封装这spring的初始化?
看一下流程图
spring开发流程
现在出现了这种关联空缺
这个xxxxxx是什么,怎么连接tomcat和MyWebApplicationInitializer方法?
最方便的方法—方法一个方法里面
public class Demo {
public static void main(String[] args) {
//操作系统的临时目录
File file=new File(System.getProperty("java.io.tmpdir"));
//启动内嵌Tomcat
Tomcat tomcat = new Tomcat();
//设置端口号
tomcat.setPort(8888);
//指定tomcat工作目录,以/来访问,file.getAbsolutePath()获取当前的绝对路径
tomcat.addContext("/",file.getAbsolutePath());
try {
//启动tomcat
tomcat.start();
AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
ac.register(WebConfig.class);
ac.refresh();
DispatcherServlet dispatcherServlet = new DispatcherServlet(ac);
Wrapper wrapper = tomcat.addServlet("/", "testServlet", dispatcherServlet);
wrapper.setLoadOnStartup(1);
wrapper.addMapping("/");
//让tomcat阻塞,等待别人连接
tomcat.getServer().await();
} catch (Exception e) {
e.printStackTrace();
}
}
}
编写测试类
@Controller
public class TestController {
@RequestMapping("/test")
@ResponseBody
public String demo(){
return "SUCCESS";
}
}
启动测试
很熟悉的场景
牛13就完事,脱离了xml配置文件。。。spring javaConfig技术
文章持续更新,可以微信搜索「 绅堂Style 」第一时间阅读,回复【资料】有我准备的面试题笔记。
GitHub https://github.com/dtt11111/Nodes 有总结面试完整考点、资料以及我的系列文章。欢迎Star。