美文网首页
SSM框架整合配置文件

SSM框架整合配置文件

作者: mark_x | 来源:发表于2019-11-21 21:20 被阅读0次

三个配置文件:

1. applicationContext.xml

  • 配置扫描dao和service,将对象的创建交给spring管理
  • spring整合mybatis,还是配置Bean对象,配置与mybatis有关的Bean。包括:
    连接池对象:dataSource
    扫描dao接口注解,创建代理对象:MapperScannerConfigurer
    执行代理dao对象增删改查方法:SqlSessionBeanFactory
  • 事务配置:使用spring的声明式事务管理
    配置事务管理器
    配置事务通知:为不同的方法(增/删/查)指定通知类型
    配置切入点:所谓切入点可以简单理解为为那些方法使用事务控制,一般就是service中所有的方法。
<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">


    <!-- 开启注解扫描,管理service和dao -->
    <!-- 注意:controller包由SpringMVC这个框架管理-->
    <context:component-scan base-package="com.itheima.ssm.service"/>
    <context:component-scan base-package="com.itheima.ssm.dao"/>

    <!--spring和mybatis整合 就是让spring管理SqlSessionFactory这个Bean-->
    <!--道理与上面一样,只不过这里需要进行依赖注入,才能创建SqlSessionFactory对象并加入到核心容器-->
    <!--1.读取数据库配置文件 硬编码写到这个xml也行,但是不方便维护-->
    <context:property-placeholder location="classpath:db.properties"/>
    <!--2.配置连接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--依赖注入,也就是想创建这个对象,所需要的构造函数参数或者set()方法参数-->
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--3.IOC管理 SqlSessionFactory-->
    <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 扫描dao包 将注解代理生成dao实现类-->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.itheima.ssm.dao"/>
    </bean>

    <!--配置Spring的声明式事务管理-->
    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--开启spring对注解事务的支持-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

2.spring-mvc.xml

  • 配置扫描controller包,与前面spring的相对应。spring管理dao和service,springmvc管理controller。
  • 配置springmvc三大组件:处理器映射器、处理器适配器、视图解析器。(注意这里不包括springmvc最核心的前端控制器DispatcherServlet,在web.xml中配置)。
    前两个通过配置注解扫描的方式自动开启
    视图解析器主要配置视图页面的前缀和后缀,注意路径不要写错!!
  • 取消对静态资源的拦截
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
           ">

    <!--扫描controller的注解 只管理controller包下的类-->
    <context:component-scan base-package="com.itheima.ssm.controller"/>

    <!--配置SpringMVC三大组件:处理器映射器、处理器适配器、视图解析器-->
    <!--相当于配置了前两者,并开启了注解支持-->
    <mvc:annotation-driven/>
    <!--配置视图解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--jsp所在目录-->
        <property name="prefix" value="/WEB-INF/pages/"/>
        <!--文件后缀名-->
        <property name="suffix" value=".jsp"/>
    </bean>


    <!--取消对访问静态资源的拦截-->
    <mvc:resources mapping="/css/" location="/css/**"/>
    <mvc:resources mapping="/img/" location="/img/**"/>
    <mvc:resources mapping="/js/" location="/js/**"/>
    <mvc:resources mapping="/plugins/" location="/plugins/**"/>

    <!--cglib代理 生成代理对象-->
    <aop:aspectj-autoproxy proxy-target-class="true"/>

</beans>

3.web.xml

  • 配置监听器,加载前面的applicationContext.xml文件
  • 编码过滤器,解决中文乱码
  • 配置SpringMVC核心Servlet
    springmvc配置文件的加载
    该servlet拦截那些请求路径,一般*.do
  <!--配置监听器-->
    <!--用于服务启动时加载配置文件-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--重新指定spring配置文件的路径-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>


    <!--编码过滤器,解决中文乱码-->
    <filter>
        <filter-name>encoding</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>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!--SpringMVC核心Servlet-->
    <!--分发器:用于各种请求和处理结果的分发
        要在服务器一启动就有,属于全局性的,因此不能在spring-mvc中配置-->
    <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:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <!--服务器启动后首先加载的页面-->
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

</web-app>

相关文章

网友评论

      本文标题:SSM框架整合配置文件

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