Spring的自动扫描、数据源配置、AOP和事务等配置

context-common.spring.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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.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-4.0.xsd
       http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd"
       default-autowire="byName">

<!-- 自动扫描 -->
<context:component-scan base-package="testwebapp.com.wangzuojia.*">
<!-- <span style="white-space:pre"></span> -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />  <!-- 不扫描标记@Controller的类,因为@Controller一般在Spring MVC的配置文件中进行配置   -->
</context:component-scan> 

<!-- 数据源配置 -->
<bean id="dataSource"
          class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/test" />
        <property name="username" value="root" />
        <property name="password" value="admin" />
        <!-- 初始化连接大小 -->  
        <property name="initialSize" value="0"></property>  
        <!-- 连接池最大数量 -->  
        <property name="maxActive" value="20"></property>  
        <!-- 连接池最大空闲 -->  
        <property name="maxIdle" value="20"></property>  
        <!-- 连接池最小空闲 -->  
        <property name="minIdle" value="1"></property>  
        <!-- 获取连接最大等待时间 -->  
        <property name="maxWait" value="60000"></property> 
        <property name="defaultAutoCommit" value="false" />
    </bean>

    <!-- 使用AspectJ方式配置AOP -->
    <aop:aspectj-autoproxy/>
    <!-- 配置事务传播特性(通知)-->
    <aop:config   proxy-target-class="true">
        <!-- 切面 -->
        <aop:pointcut id="servicePointcut"
                      expression="execution(* testwebapp.com.wangzuojia.service..*.*(..))" />
        <!-- 事务处理方法  order="1"-->
        <aop:advisor pointcut-ref="servicePointcut" advice-ref="txAdvice"/>
    </aop:config>
    <!--配置哪些类的哪些方法参与事务 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!-- 定义事务管理的规则 -->
        <tx:attributes>
            <tx:method name="add*" />
            <tx:method name="insert*" />
            <tx:method name="remove*" />
            <tx:method name="save*" />
            <tx:method name="update*" />
            <tx:method name="delete*" />
        </tx:attributes>
    </tx:advice>

    <!-- 配置事务管理器  -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <!-- 申明式事务 --><!-- 可通过注解控制事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- mybatsi相关 --><!-- 创建SqlSessionFactory,同时指定数据源 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
        <property name="mapperLocations">
            <array>
                <value>classpath*:testwebapp/com/wangzuojia/mapping/*.xml</value><!-- * 匹配0或者任意数量的字符;** 匹配0或者更多的目录 -->
            </array>
        </property>
        <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="testwebapp.com.wangzuojia.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
    
</beans>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值