ssm的spring配置文件分析

spring配置文件

1.开启注解模式 可以不写 默认就是开启的

 <!--开启注解模式-->
<context:annotation-config></context:annotation-config>

2.定义注解扫描的包

注意不要和springmvc中的冲突

<!--定义注解扫描的包-->
<context:component-scan base-package="com.xpc.pojo,com.xpc.util,com.xpc.service,com.xpc.mapper"></context:component-scan>

3.开启aop注解功能

如果不使用 也可以不配置 一般都会使用

<!--开启aop注解功能-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

4.加载db.peroperties配置文件

<!--加载db.peroperties配置文件-->
    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>

5.通过spring创建一个数据源(连接池)

ComboPooledDataSource//c3p0连接池
<bean name="dataSource"
      class="com.mchange.v2.c3p0.ComboPooledDataSource"
      p:jdbcUrl="${jdbc.jdbcUrl}"
      p:driverClass="${jdbc.driverClass}"
      p:user="${jdbc.user}"
      p:password="${jdbc.password}">
</bean>

6.配置平台事务管理器

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

7.创建SqlSessionFactory工厂

通过SqlSessionFactoryBean创建

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
   <!--数据源-->
    <property name="dataSource" ref="dataSource"></property>
   <!--mybatis核心配置文件位置-->
    <property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>

8.配置mapper接口所在位置

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.xpc.mapper"></property>
</bean>

9.事务切面

使用注解

<!--启用事务注解模式-->
<tx:annotation-driven></tx:annotation-driven>

使用配置文件

<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <!-- 传播行为 -->
        <!-- REQUIRED:如果有事务,则在事务中执行;如果没有事务,则开启一个新的事物 -->
        <tx:method name="save*" propagation="REQUIRED" />
        <tx:method name="insert*" propagation="REQUIRED" />
        <tx:method name="add*" propagation="REQUIRED" />
        <tx:method name="create*" propagation="REQUIRED" />
        <tx:method name="delete*" propagation="REQUIRED" />
        <tx:method name="update*" propagation="REQUIRED" />
        <tx:method name="transfer" propagation="REQUIRED" />
        <!-- SUPPORTS:如果有事务,则在事务中执行;如果没有事务,则不会开启事物 -->
        <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
        <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
        <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
    </tx:attributes>
</tx:advice>
<!--配置事务的植入过程-->
<aop:config>
    <!--切入点-->
    <aop:pointcut id="txPointCut" expression="execution(* com..service..*.*(..))" />
    <!--配置切入点和切面关联-->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
ibutes>
</tx:advice>

猜你喜欢

转载自blog.csdn.net/Riding_ants/article/details/106713001
今日推荐