一、引言
手动创建一个 Spring 容器,然后调用容器的 getBean() 方法获取Spring容器中对应的Bean:
public static void main(String[] args) {
ApplicationContext apx = new ClassPathXmlApplicationContext("bean-factory.xml");
Car car = (Car) apx.getBean("car");
System.out.println(car);
}
但在现实的项目开发中,我们不会手动创建容器。比如,在Web工程中,我们根本不用管 Spring 容器的启动和创建,只需要在web.xml文件中配置一下,就可以使用 Spring 提供的能力。
二、Tomcat项目是如何启动的?
- 在启动 Web 项目时,容器(比如 Tomcat )会读取 web.xml
配置文件中的两个节点和;那 Tomcat 为什么会读取呢?因为
ServletContextListener - 接着 Tomcat 会创建一个 ServletContext (上下文) 对象,该对象的应用范围,是整个 Web 项目都能使用这个上下文;
- 接着 Tomcat 会将读取到转化为键值对,并交给 ServletContext;
- 接着 Tomcat会创建 中的类实例,即创建监听器。该监听器能够监听 ServletContext对象的生命周期,实际上就是监听 Web 应用的生命周期。
- 当Tomcat启动或终止时,会触发ServletContextEvent事件,该事件由ServletContextListener来处理。
ServletContextListener有下面两个方法:
1)、初始化方法:contextInitialized(ServletContextEvent event)
在这个方法中可以通过event.getServletContext().getInitParameter("XXXXX") ,来得到context-param设定的值;
2)、销毁方法: contextDestroyed(ServletContextEvent event)
在这个方法中,多用于关闭应用前释放资源,比如说数据库连接的关闭;
得到这个 context-param 的值之后,你就可以做一些操作了。
注意,这个时候 Web 项目还没有完全启动完成,这个动作会比所有的Servlet都要早!
三、Tomcat启动时如何集成Spring?
1. web.xml配置文件
<!-- 定义了WEB应用的名字-->
<display-name>ssm</display-name>
<!-- 设置欢迎页-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--===============================配置Spring==========================================================-->
<!--
Tomcat 会将读取到<context-param>转化为键值对,并交给 ServletContext
就是,将配置文件applicationContext.xml交给ServletContext对象
启动spring容器,需要加载必要的配置文件applicationContext.xml
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--
监听器:public class ContextLoaderListener extends ContextLoader implements ServletContextListener
1、tomcat启动,
2、servletcontext被创建,
3、监听器(ContextLoaderListener)执行contextInitialized(ServletContextEvent event)方法
4、这个方法中可以通过event.getServletContext().getInitParameter("XXXXX") ,来得到context-param设定的值(配置文件applicationContext.xml);
5、加载spring的配置文件,启动spring容器
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--====================================配置springMvc的前端控制器==========================================================-->
<!-- 2,配置springMvc的前端控制器,拦截所有请求,DispatcherServlet是一个负责调度工作的servlet,控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定配置文件位置和名称 如果不设置,默认找/WEB-INF/<servlet-name>-servlet.xml -->
<init-param>
<!--Spring Web MVC框架将加载“classpath:spring-servlet-config.xml”
来进行初始化上下文而不是“/WEB-INF/[servlet名字]-servlet.xml
param-name表示传入参数,key+value,可通过config.getInitParameter来获取 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<!-- 启动spring容器时初始化该servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 过虑所有,servlet-mapping表示配置其 URL,配置了才能响应用户请求-->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--======================================处理POST提交乱码的问题============================================================-->
<!-- 3,字符编码过滤器,放在所有过滤器的前面-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--==============================================Rest风格的URI===============================================================-->
<!-- 4,过滤器:Rest风格的URI,将页面普通的post请求转为指定的delete或者put请求-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/69812.html