How Tomcat container is to start, to create and integrate the Spring container?

I. Introduction
manually create a Spring container, then call the container getBean () method to get the Spring container corresponding Bean:

 public static void main(String[] args) {
        ApplicationContext apx = new ClassPathXmlApplicationContext("bean-factory.xml");
        Car car = (Car) apx.getBean("car");
        System.out.println(car);
    }

But in the real development of the project, we will not manually create the container. For example, in a Web project, we do not control and start to create a Spring container, just configure it in web.xml, Spring provides the ability can be used.

Two, Tomcat project is how to start?

  • When you start a Web project, containers (such as Tomcat) will read the web.xml
    two node configuration file and; why that Tomcat will read it? Because
    ServletContextListener
  • Then Tomcat creates a ServletContext (context) object, the application range of the object, the entire project can use the Web context;
  • Tomcat will then read into key-value pairs and to the ServletContext;
  • Tomcat will then create a class instance that created the listener. The listener can listen lifecycle ServletContext object is actually listening lifecycle of Web applications.
  • When Tomcat starts or terminates, triggering ServletContextEvent event, which is handled by ServletContextListener.
ServletContextListener有下面两个方法:
1)、初始化方法:contextInitialized(ServletContextEvent event)
在这个方法中可以通过event.getServletContext().getInitParameter("XXXXX") ,来得到context-param设定的值;
2)、销毁方法:  contextDestroyed(ServletContextEvent event)
在这个方法中,多用于关闭应用前释放资源,比如说数据库连接的关闭;

After obtaining the value of context-param, you can do some of the operations.
Note that this time the project has not been fully Web startup is complete, this action will be earlier than all of the Servlet!

Third, how to integrate Tomcat starts when the Spring?
1. the web.xml configuration file


<!--  定义了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>
发布了2 篇原创文章 · 获赞 2 · 访问量 45

Guess you like

Origin blog.csdn.net/weixin_44730681/article/details/104072077
Recommended