美文网首页
2018-07-20 基础之ssm框架的配置文件

2018-07-20 基础之ssm框架的配置文件

作者: sunnysans | 来源:发表于2018-07-20 10:35 被阅读0次

自己理解(口语化以及借鉴别人的,自己理解)


ssm框架的整合

1.web.xml解剖

a.启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点

  1. 监听器<listener></listener>

    • 写法

        <listener>
        <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
      
    • 看ContextLoaderListener类的源码

        public class ContextLoaderListener 
        extends ContextLoader 
        implements ServletContextListener {}
      
    • 继承于ContextLoader类。

    • 实现 ServletContextListener接口

  2. <context-param></context-param>

    • 写法

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>  
        </context-param>
      
    • 自己理解就是加载applicationContext.xml文件,也就是spring.xml。

b.容器创建一个ServletContext(上下文),这个WEB项目所有部分都将共享这个上下文

  1. 创建过程:

     public void contextInitialized(ServletContextEvent event) {
       initWebApplicationContext(event.getServletContext());
         }
    
  2. 类ContextLoaderListener实现ServletContextListener接口所实现的方法

c.容器将<context-param></context-param>转化为键值对,并交给ServletContext.

d.容器创建<listener></listener>中的类实例,即创建监听.


a-d是启动一个WEB项目的时候,加载web.xml读取其中的两个节点。读取原理


e.在监听中会有contextInitialized(ServletContextEvent args)初始化方法,在这个方法中获得ServletContext = ServletContextEvent.getServletContext();context-param的值 = ServletContext.getInitParameter("context-param的键");

f.得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早.

g..你可能想在项目启动之前就打开数据库.那么这里就可以在<context-param>中设置数据库的连接方式,在监听类中初始化数据库的连接.

h.这个监听是自己写的一个类,除了初始化方法,它还有销毁方法.用于关闭应用前释放资源.比如说数据库连接的关闭.


e-h是具体的一些应用


2.web.xml常用写法

  • 读取web.xml文件时加载的节点

    • 上下文参数

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>  
        </context-param>
      
    • 监听器

        <listener>
            <listener-class>
            org.springframework.web.context.ContextLoaderListener
            </listener-class>
        </listener>
      
  • 其他

      <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>forceEncoding</param-name>
              <param-value>true</param-value>
          </init-param>
      </filter>
      <filter-mapping>
          <filter-name>characterEncodingFilter</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
      
      <!-- 声明监听器,在tomcat启动时创建spring容器 -->
      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      
      <!-- 把所有的请求都交给springmvc容器 -->
      <servlet>
          <servlet-name>springmvc</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:springmvc.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
          <servlet-name>springmvc</servlet-name>
          <url-pattern>/</url-pattern>
      </servlet-mapping>
    
  • Listener,Filter和servlet都是配置在web.xml文件中。这三个组成传统意义上的servlet

    1. 启动的顺序为listener->Filter->servlet.

    2. Listener生命周期:一直从程序启动到程序停止运行。

      ServletRequestListener:每次访问一个Request资源前,都会执行requestInitialized()方法,方法访问完毕,都会执行requestDestroyed()方法。

      HttpSessionListener:每次调用request.getSession(),都会执行sessionCreated()方法,执行session.invalidate()方法,都会执行sessionDestroyed()方法。

      ServletRequestAttributeListener:每次调用request.setAttribute()都会执行attributeAdded()方法,如果set的key在request里面存在,就会执行attributeReplacerd()方法,调用request.removeAttribute()方法,都会执行attributeRemoved()方法。

3. Filter生命周期:程序启动调用Filter的init()方法(永远只调用一次,具体看启动日志),程序停止调用Filter的destroy()方法(永远只调用一次,具体看关闭日志),doFilter()方法每次的访问请求如果符合拦截条件都会调用(程序第一次运行,会在servlet调用init()方法以后调用,不管第几次,都在调用doGet(),doPost()方法之前)。



4. Servlet生命周期:程序第一次访问,会调用servlet的init()方法初始化(只执行一次,具体看日志),每次程序执行都会根据请求调用doGet()或者doPost()方法,程序停止调用destory()方法(具体看结束日志)。
  • welcome页面以及报错页面的书写

      <error-page>
          <!-- 错误码 -->
          <error-code>404</error-code>
          <location>/WEB-INF/jsp/404.jsp</location>
      </error-page>   
    
      <welcome-file-list>
          <welcome-file>/WEB-INF/jsp/login.jsp</welcome-file>
      </welcome-file-list>
    

2.springmvc.xml解析

读取web.xml文件后,会根据里面的配置分别去寻找各自的配置文件

<?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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans.xsd 
            http://www.springframework.org/schema/context 
            http://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/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 配置扫描的包
    spring可以自动去扫描base-package下面或者子包下面的java文件,
    如果扫描到有@Component @Controller @Service @Repository等这些注解的类,则把这些类注册为bean -->
    <context:component-scan base-package="com.ajiatech.*" />

    <!-- 注册HandlerMapper、HandlerAdapter两个映射类 -->
    <mvc:annotation-driven />
    <!-- 在web.xml中springmvc拦截了所有请求,包括静态资源的请求,Spring MVC会将它们当成一个普通请求处理,因此找不到对应处理器将导致错误。下面设视是spring3.0后改进的,当springmvc拦截了所有的请求以后,会在Spring MVC上下文中定义一个org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler,它会像一个检查员,对进入DispatcherServlet的URL进行筛查,如果发现是静态资源的请求,就将该请求转由Web应用服务器默认的Servlet处理,如果不是静态资源的请求,才由DispatcherServlet继续处理。 -->
    <!-- 访问静态资源 -->

    <!--会自动注册RequestMappingHandlerMapping与RequestMappingHandlerAdapter两个Bean,这是Spring MVC为@Controller分发请求所必需的,并且提供了数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持读写XML的支持(JAXB)和读写JSON的支持(默认Jackson)等功能。

    使用该注解后的springmvc-config.xml:-->
    <mvc:default-servlet-handler />

    <!-- 消息转换器 当springmvc的controller返回对象时,执行消息转换器, 把返回的java对象转成json字符串,输出贵客户端。 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean id="mappingJacksonHttpMessageConverter"
                class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes" value="text/html;charset=UTF-8" />
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

<!-- 视图解析器 Controller处理完毕后将会转发到相应位置 prefix前缀 suffix后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

3.applicationContext.xml解析

  • spring:业务逻辑组件

    • 1.建一个jdbc.properties,先引入,数据源,事务控制,xxx。
    • 2,扫描业务逻辑组件包(不扫描控制器)
    • 3.配置Mybatis的整合:SqlSessionFactoryBean
      指定mybatis全局配置文件的位置
      指定mybatis,mapper文件的位置
    • 4.配置扫描器,将mybatis接口的实现加入到ioc容器中
      扫描所有DAO接口,加入到ioc容器中
    • 5.事务控制配置
      控制数据源
    • 6.开启基于注解的事物或者使用xml配置形式的事物(主要使用配置式)
      1.切入点表达式
      2.配置事物增强
      3.配置事务如何增强;事物如何切入
      所有方法都是事物方法
      以get开始的所有方法
  • 配置文件

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:context="http://www.springframework.org/schema/context"
          xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.springframework.org/schema/beans   
                  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                  http://www.springframework.org/schema/context 
                  http://www.springframework.org/schema/context/spring-context-2.5.xsd    
                  http://www.springframework.org/schema/aop    
                  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
      
          <!-- 扫描Spring除Controller之外的注解 -->
          <context:component-scan base-package="com.ajiatech">
              <context:exclude-filter type="annotation"  expression="org.springframework.stereotype.Controller" />
          </context:component-scan>
      
          <!-- 配置数据源 -->
          <bean id="jdbcDataSource"
              class="org.springframework.jdbc.datasource.DriverManagerDataSource">
              <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
              <property name="url"
                  value="jdbc:mysql://localhost:3306/ajia_store?useSSL=true&amp;characterEncoding=UTF8"></property>
              <property name="username" value="root"></property>
              <property name="password" value="root"></property>
          </bean>
      
          <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
          <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
              <property name="dataSource" ref="jdbcDataSource" />
              <property name="mapperLocations" value="classpath:com/ajiatech/mapper/*.xml"></property>
          </bean>
      
      <!-- 加载mapper接口 代理对象 -->
      <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
          <property name="basePackage" value="com.ajiatech.mapper"></property>
      </bean>
      
      </beans>
    

写法

根据url找到对应的servlet后,根据注解进入对应的controller中,传参,获取想要的对象,调用service的方法。具体的实现在serviceImpl中书写,new出查询语句。返回查询的结果,然后进行视图渲染

  • 文件

    1. *Mapper.java 一些增删改查的方法
    2. *Mapper.xml 具体方法的实现 sql语句
  • pojo 文件夹

    1. *.java 数据表的结构 与set。get方法
    2. *Example.java 用于添加sql里面的添加(eg:where)
  • service 文件夹

    1. *service.java 方法的提出
    2. *serviceImpl.java 方法的具体实现。写增删改查
  • controller

    • url找到controller

注解

  1. @Service用于标注业务层组件
  2. @Controller用于标注控制层组件(如struts中的action)
  3. @Repository用于标注数据访问组件,即DAO组件
  4. @RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

相关文章

网友评论

      本文标题:2018-07-20 基础之ssm框架的配置文件

      本文链接:https://www.haomeiwen.com/subject/ehefmftx.html