Spring整合Web
刚刚看了一下Spring整合Web,趁着还有点记忆,赶紧写点总结来巩固一下。(此处举例开发工具Myeclipse,tomcat服务器,以及未用到的MySql)
个人认为Spring整合Web,其实就是将Spring中的配置文件在Tomcat启动的时候初始化.
在整合之前,要导入对应的jar包----Spring-web.jar.
1.Tomcat加载配置文件初始化
有三种方式
servlet–>init(ServletConfig)–>load-on-startup
filter–>init(FileConfig)–>web.xml注册过滤器自动调用初始化。
以及下面的第三种
此处我们用的是listener–>ServletContextListener–>ServletContext对象监听。
确定加载文件位置
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
接下来就是从servletContext作用域中去获取Spring容器了
这里也有两种方式
①手动获取(需要强转)
ApplicationContext application = (ApplicationContext)this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
②通过Utils工具获取
ApplicationContext application = WebApplicationContextUtils.getWebApplicationContext(this.getServlet());
最后推荐一下B站一个学习Spring的Up主(
一个体面的人)再附上学习视频连接方便你们找到他。讲的很细,个人觉得相当不错。https://www.bilibili.com/video/BV1nx411g7ja?p=39