ssm integration configuration, understanding of the role of each layer of ssm, interpretation of configuration files of each layer of ssm, web program startup and web.xml understanding

Tomcat server load order Tomcat startup When tomcat starts, it will load the web.xml file. When loading the web.xml file...

Tomcat server load order

Tomcat startup

When tomcat starts, it will load the web.xml file. When loading the web.xml file, it will read many configuration parameters together, such as,, etc. there are two kinds of servlet loading, one is to load when accessing, and the other is to load when the server starts (only need to be set to 1).

Loading process of web.xml

In the web app element, the configuration order of the elements is independent of the loading order of the project. The loading process of web.xml is as follows:

  1. Start a web project, and the web container (such as tomcat) reads the web.xml file and the configuration information in it

  2. The container creates a servlet context that is shared by all parts of the web project

  3. The container will be converted into a key value pair and handed over to servletContext

  4. Create listener instances in the container

  5. The contextInitialized method is triggered, and the listener is called (when the Servlet container starts or terminates the Web application, it will trigger the ServletContextEvent event event, which is handled by the ServletContextListener. The ServletContextListener interface defines two methods for handling the ServletContextEvent event event: contextInitialized; contextDestroyed, web.xml, contextLoaderListener listener, spring and other frameworks, which implement the connection of this listener Mouth method)

  6. After calling the contextInitialized method, the container initializes the filter

SSM stratification

  1. Spring MVC: it is responsible for the View layer View, which is equivalent to the native servlet in the Java Web stage. It is mainly used for page request forwarding and View management. Generally, spring MVC scans the controller package.

Spring MVC components include: front-end controller, processor mapper, processor adapter, View parser, processor Handler and View view. Only processor Handler and View need to be developed by programmers.

  1. Spring: implements object management, which is used to manage beans, IOC containers, DI and AOP

  2. Mybatis: it is responsible for the interaction design with the database to deal with the persistence of data. It is simply understood as some JDBC operations in the Java Web stage, etc.

Spring MVC configuration

**The core of the spring MVC framework is actually a Servlet, but it uses one Servlet to manage all requests. Therefore, its configuration is to add the spring MVC Servlet distributor to web.xml. * * as follows:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- =====================Scan component====================================--> <!--SpringMVC The configuration file contains the control and configuration of website jump logic --> <context:component-scan base-package="com.nju" use-default-filters="false"> <!--Scan only the controller, i.e@Controller annotation --> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--===================Configure the view parser (you can also use other)===================== --> <bean id="viewResolver"> <property name="order" value="1"/> <property name="characterEncoding" value="UTF-8"/> <property name="templateEngine"> <bean> <property name="templateResolver"> <bean> <!-- View prefix --> <property name="prefix" value="/WEB-INF/templates/"/> <!-- View suffix --> <property name="suffix" value=".html"/> <property name="templateMode" value="HTML5"/> <property name="characterEncoding" value="UTF-8" /> </bean> </property> </bean> </property> </bean> <!-- Default view parser--> <!-- <bean id="viewResolver">--> <!-- &lt;!&ndash; Configure prefix for logical view &ndash;&gt;--> <!-- <property name="prefix" value="/WEB-INF/templates/" />--> <!-- &lt;!&ndash; Configure suffix for logical view &ndash;&gt;--> <!-- <property name="suffix" value=".html" />--> <!-- </bean>--> <!--===================Configure the view parser (you can also use other)===================== --> <!-- take springmvc Requests that cannot be processed are handed over to tomcat Processing static resources, such as html,js,css,jpg stay WEB When the container is started, a DefaultServletHttpRequestHandler, It will be right DispatcherServlet If the request has been mapped, it will then be handed over to the corresponding handler in the background, If there is no mapping, give it to WEB Application server default Servlet Processing to find the corresponding static resource. An error will be reported only when the resource can no longer be found. Original link: https://blog.csdn.net/codejas/article/details/80055608 --> <mvc:default-servlet-handler/> <!-- ========================open mvc Annotation driven================================= --> <mvc:annotation-driven> <mvc:message-converters> <!-- Processing response Chinese content garbled --> <bean> <property name="defaultCharset" value="UTF-8" /> <property name="supportedMediaTypes"> <list> <value>text/html</value> <value>application/json</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!--View controller (optional)--> <mvc:view-controller path="/" view-name="index"></mvc:view-controller> </beans>

Integrated configuration of Spring and Mybatis

First, Spring needs its own configuration file, which is generally named applicationContext.xml.

Because spring is the core of SSM framework, it needs to be loaded and run when the server starts. Generally, only the web.xml file can be used for the files that can be loaded when the server starts. Therefore, the spring loading settings are still placed in the web.xml file.

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!--==========================Scan component==========================--> <!--Note: the difference here is SpringMVC Configuration file, here*no*scanning Controller controller--> <context:component-scan base-package="com.nju"> <!--*no*scanning Controller controller--> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--======================Spring integration Mybatis======================--> <!--====1,Configure connection pool, use here druid===--> <!--load file--> <context:property-placeholder location="classpath:DruidDB.properties" /> <bean id="pooledDataSource"> <!--Database related configuration information can also be imported through external files--> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/> <property name="username" value="root"/> <property name="password" value="12345678"/> </bean> <!--===2,to configure SqlSessionFactory,This factory is used to create dao The proxy object of the interface needs to reference the data source-===--> <bean id="sqlSessionFactory"> <!-- appoint Mybatis Location of configuration file--> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!--specify data source druid--> <property name="dataSource" ref="pooledDataSource"/> <!--appoint*mapper.xml Location of--> <property name="mapperLocations" value="classpath*:/mapper/*.xml"/> </bean> <!--===3,to configure dao The location of the interface object, dao Mapping location====--> <!-- Configure the scanner to mybatis Interface implementation added to ioc In container --> <bean> <!--Scan all dao Interface implementation, added to ioc In container --> <property name="basePackage" value="com.nju.mapper"/> </bean> <!--====4,(Configure a batch that can be executed according to the actual demand sqlSession======--> <bean id="sqlSession"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/> <constructor-arg name="executorType" value="BATCH"/> </bean> <!--=====================to configure spring Framework declarative transaction management====================--> <!--1. Create transaction manager--> <bean id="transactionManager"> <!--Injection data source--> <property name="dataSource" ref="pooledDataSource"/> </bean> <!--2 Configure notifications, configure transaction enhancements--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!--All methods are transaction methods--> <tx:method name="*"/> <!--with get Start all methods--> <tx:method name="get" read-only="true"/> </tx:attributes> </tx:advice> <!--3 Configure pointcuts and facets--> <aop:config> <!--Configure pointcut expressions--> <aop:pointcut id="txPoint" expression="execution(* com.nju.service..*(..))"/> <!--Configure transaction enhancements--> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/> </aop:config> </beans>

mybaits profile

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!--Hump naming--> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <typeAliases> <!--Set alias--> <package name="com.nju.bean"/> </typeAliases> <plugins> <!--Paging plug-in--> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!--Paging parameter rationalization --> <property name="reasonable" value="true"/> </plugin> </plugins> </configuration>

Configuration of web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- ==================== start-up Spring ================== --> <!-- Server startup is the loaded parameter, set spring xml File location --> <!-- context-param The label will be web Container priority reading is converted into key value pairs and handed over to servletContext,To obtain the value of the key value pair, you can servlet in use getServletContext().getInitParameter obtain. Here and servlet Inside init-param Similar, but context-param yes be-all servlet Shared. --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- ====================Configure listener======================= --> <listener> <!--ContextLoaderListener It can capture the start and stop of the server, and do the corresponding operation in the start and stop trigger--> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- ====================start-up SpringMVC==================== --> <!-- In fact, the practice and configuration of a native servlet Similar, just use SpringMVC Only one needs to be configured Servlet--> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- Specified by initialization parameters SpringMVC Location and name of the configuration file --> <init-param> <!-- contextConfigLocation Is a fixed value --> <param-name>contextConfigLocation</param-name> <!-- use classpath:Indicates finding a configuration file from a classpath, for example maven In Engineering src/main/resources --> <param-value>classpath:SpringMVC.xml</param-value> </init-param> <!-- As the core component of the framework, there are a lot of initialization operations to be done during the startup process, and these operations are executed only when the first request is made, which will seriously affect the access speed Therefore, it is necessary to pass load-on-startup The tag will initiate control DispatcherServlet The initialization time of is advanced until the server starts --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <!-- set up springMVC The request path of the request that can be processed by the core controller of /The matching request can be/login or.html or.js or.css Mode request path however/Cannot match.jsp Request for request path --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- ================= filter filter ====================== --> <!-- Character encoding filters must be placed before all filters --> <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> <!-- ================= Restful((optional) ====================== --> <!-- 4,use Rest Stylized URI,Convert the page to normal post Request to convert to specified delete perhaps put request --> <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> <filter> <filter-name>HttpPutFormContentFilter</filter-name> <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class> </filter> <filter-mapping> <filter-name>HttpPutFormContentFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>

maven configuration (some jar packages are optional)

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.nju</groupId> <artifactId>ssm-crud</artifactId> <version>1.0-SNAPSHOT</version> <!--web engineering--> <packaging>war</packaging> <!--Import project dependent jar package --> <dependencies> <!--introduce pageHelper Paging plug-in --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.0.0</version> </dependency> <!-- MBG: realization Mybaits reverse engineering --> <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> <!-- SpringMVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.1</version> </dependency> <!-- journal --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <!-- ServletAPI --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- Spring5 and Thymeleaf Integration package --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>3.0.12.RELEASE</version> </dependency> <!-- Spring-Jdbc --> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!--Spring-test --> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!-- Spring Aspect oriented programming aspects --> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!--MyBatis --> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.2</version> </dependency> <!-- MyBatis integration Spring Adaptive package for --> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- Database connection pool, driver --> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!--mysql--> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!-- (jstl,servlet-api,junit) --> <!-- https://mvnrepository.com/artifact/jstl/jstl --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <!-- junit --> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> <!--Resolve problems during testing: FAILED TO EXECUTE GOAL ORG.APACHE.MAVEN.PLUGINS:MAVEN-SUREFIRE-PLUGIN:2.12.4--> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.4.2</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>

reference resources

https://blog.csdn.net/qq_17275841/article/details/88830334

https://www.freesion.com/article/4166575533/

https://blog.csdn.net/weixin_42176453/article/details/80263774

29 November 2021, 11:20 | Views: 9783

Add new comment

For adding a comment, please log in
or create account

0 comments