• SpringMVC 学习 九 SSM环境搭建 (二) Spring配置文件的编写


    spring配置文件中需要干的事情

    (一)开启  Service与pojo包的注解扫描

    注意:spring 扫描与表对应的实体类,以及service层的类,不能用来扫描Controller层的类,因为Controller层的类需要由SpringMVC容器来管理,如果采用了Spring容器管理,就会产生声明式事物无效

    <context:component-scan base-package="com.ssm.pojo,com.ssm.bean,com.ssm.service.impl"></context:component-scan>
        

    (二)spring整合mybatis的一些配置

    (1)加载properties配置文件

    <context:property-placeholder location="classpath:db.properties"/>

    (2)配置数据源

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
              <property name="driverClassName" value="${jdbc.driver}"></property>
              <property name="url" value="${jdbc.url}"></property>
              <property name="username" value="${jdbc.username}"></property>
              <property name="password" value="${jdbc.password}"></property>
        </bean>

    (3)配置sqlSessionFactory

     <!-- 配置sqlSessionFactory -->
        <!-- sqlSessionFactory ,注册sqlSession工厂-->
         <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
             <!--dataSource的类型是DataSource是一个接口,上面配置的DriverManagerDataSouce类实现了DataSource接口  -->
             <property name="dataSource" ref="dataSource"></property>
             <!-- 设置别名 -->
             <property name="typeAliasesPackage" value="com.ssm.pojo"></property>
         </bean>

    (4)配置扫描器,用来扫描mybatis中xxxMapper.xml文件以及xxxMapper.java接口文件,并使用cglib的动态代理生成接口的实现类

     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
                <property name="basePackage" value="com.ssm.mapper" ></property>
                <!-- <property name="sqlSessionFactory" ref="factory"></property> -->
                <property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
          </bean>

    (三)在开发中需要用到spring的声明式事物,配置事物

    (1)配置事物管理器

        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
             <property name="dataSource" ref="dataSource"></property>
          </bean>

    (2)配置事物

    <!-- 配置声明式事物,声明式事物需要和Aop一起使用,实际工作中会开启注解 -->
          <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <!-- 哪些方法需要有事务控制 -->
                <!-- 方法以 ins 开头事务管理 -->
                <tx:method name="ins*" />
                 <!-- 方法以 del 开头事务管理 -->
                <tx:method name="del*" />
                 <!-- 方法以 upd 开头事务管理 -->
                <tx:method name="upd*" />
                <!-- readOnly
                事务属性中的readOnly标志表示对应的事务应该被最优化为只读事务。
                如果值为true就会告诉Spring我这个方法里面没有insert或者update,
                你只需要提供只读的数据库Connection就行了,
                这种执行效率会比read-write的Connection高,
                所以这是一个最优化提示。
                在一些情况下,一些事务策略能够起到显著的最优化效果
                ,例如在使用Object/Relational映射工具
                (如:Hibernate或TopLink)时避免dirty checking(试图“刷新”)。 -->
                <tx:method name="*" read-only="true"/>
            </tx:attributes>
        </tx:advice>

    (3)配置切点和切面,注意execution里面的编写方法,返回值要与方法全路径之间有空格

    <!-- 
                声明式事物是基于AOP的,因此要声明切点和通知
         -->
        <aop:config>
            <!-- 切点范围设置大一些 -->
            <aop:pointcut expression="execution(* com.ssm.service.impl.*.*(..))" id="mypoint" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint" />
        
        </aop:config>

    (4)cglib的配置,如果在代码中有需要把代理类对象赋值给目标类对象的代码,此时动态代理需要使用cglib的动态代理,如果没有需要把代理对象赋值给目标对象,可以不用开启cglib

    <!-- 
            AOP的注解方式都是采用cglib动态代理
             proxy-target-class="true"代表采用cglib动态代理
             false代表使用jdk的动态代理
         -->
        <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>

    配置文件全文:

    <?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:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        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/aop 
            http://www.springframework.org/schema/aop/spring-aop.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">
        <!-- 开启注解
            注意:spring 扫描与表对应的实体类,以及service层的类,不能用来扫描Controller层的类,
            因为Controller层的类需要由SpringMVC容器来管理,如果采用了Spring容器管理,
            就会产生声明式事物无效
         -->
        <context:component-scan base-package="com.ssm.pojo,com.ssm.bean,com.ssm.service.impl"></context:component-scan>
        
        <!-- 加载属性文件 -->
        <context:property-placeholder location="classpath:db.properties"/>
        <!-- 配置数据源 -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
              <property name="driverClassName" value="${jdbc.driver}"></property>
              <property name="url" value="${jdbc.url}"></property>
              <property name="username" value="${jdbc.username}"></property>
              <property name="password" value="${jdbc.password}"></property>
        </bean>
        
        <!-- 配置sqlSessionFactory -->
        <!-- sqlSessionFactory ,注册sqlSession工厂-->
         <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
             <!--dataSource的类型是DataSource是一个接口,上面配置的DriverManagerDataSouce类实现了DataSource接口  -->
             <property name="dataSource" ref="dataSource"></property>
             <!-- 设置别名 -->
             <property name="typeAliasesPackage" value="com.ssm.pojo"></property>
         </bean>
        
        <!-- 扫描器,用来扫描mybatis中xxxMapper.xml文件以及xxxMapper.java接口文件,并使用cglib的动态代理生成接口的实现类 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
                <property name="basePackage" value="com.ssm.mapper" ></property>
                <!-- <property name="sqlSessionFactory" ref="factory"></property> -->
                <property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
          </bean>
          
          <!-- 配置事物管理器 -->
          <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
             <property name="dataSource" ref="dataSource"></property>
          </bean>
          
          
          <!-- 配置声明式事物,声明式事物需要和Aop一起使用,实际工作中会开启注解 -->
          <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <!-- 哪些方法需要有事务控制 -->
                <!-- 方法以 ins 开头事务管理 -->
                <tx:method name="ins*" />
                 <!-- 方法以 del 开头事务管理 -->
                <tx:method name="del*" />
                 <!-- 方法以 upd 开头事务管理 -->
                <tx:method name="upd*" />
                <!-- readOnly
                事务属性中的readOnly标志表示对应的事务应该被最优化为只读事务。
                如果值为true就会告诉Spring我这个方法里面没有insert或者update,
                你只需要提供只读的数据库Connection就行了,
                这种执行效率会比read-write的Connection高,
                所以这是一个最优化提示。
                在一些情况下,一些事务策略能够起到显著的最优化效果
                ,例如在使用Object/Relational映射工具
                (如:Hibernate或TopLink)时避免dirty checking(试图“刷新”)。 -->
                <tx:method name="*" read-only="true"/>
            </tx:attributes>
        </tx:advice>
        <!-- 
                声明式事物是基于AOP的,因此要声明切点和通知
         -->
        <aop:config>
            <!-- 切点范围设置大一些 -->
            <aop:pointcut expression="execution(* com.ssm.service.impl.*.*(..))" id="mypoint" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint" />
        
        </aop:config>
        <!-- 
            AOP的注解方式都是采用cglib动态代理
             proxy-target-class="true"代表采用cglib动态代理
             false代表使用jdk的动态代理
         -->
        <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
        
    </beans>
  • 相关阅读:
    高可靠JAVA项目
    C语言JS引擎
    星际争霸,FF反作弊对战平台
    【转】ffluos编译
    〓经典文字武侠游戏 书剑 书剑江湖自由度超高!公益服!〓
    全局解释器锁GIL
    创建多线程Thread
    线程的简述Thread
    进程池的回调函数callback
    进程池的同步与异步用法Pool
  • 原文地址:https://www.cnblogs.com/cplinux/p/9821232.html
Copyright © 2020-2023  润新知