spring-bean

2018-09-06  本文已影响0人  别叫我小新

<?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"
    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/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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 
  
    <!-- 注解扫描  扫描指定包及其子包下的注解 -->
   <context:component-scan base-package="com.qianfeng">
        <!-- 排除扫描的注解,排除controller层,不和mvc冲突 -->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
   </context:component-scan>
  
   <!-- 数据源的配置 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/employee"></property>
        <property name="user" value="root"></property>
        <property name="password" value="lin"></property>
        <property name="initialPoolSize" value="5"></property>
        <property name="maxPoolSize" value="10"></property>
        <property name="maxIdleTime" value="1000"></property>
    </bean>
    
    <!-- 通过Spring创建Hibernate的SessionFactory对象 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- 将原来hibernate.cfg.xml的配置直接写入spring的配置 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        
        
        <!-- 扫描hibernate注解 -->
        <property name="packagesToScan" value="com.qianfeng.entity"></property>
    </bean>
   
  <!-- 1配置hibernate的事务管理类 -->
    <bean id="txManage" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <!-- 注入sessionFactory -->
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
   
    <!-- 2配置事务的特性 -->
   <tx:advice id="txAdvice" transaction-manager="txManage">
        <tx:attributes>
            <!-- 针对使用事务的add开头的方法 
            read-only 是否只读,true 是,false 可读可写
            如果有插入等操作,设为为true,运行程序会报异常-->
            <tx:method name="add*" read-only="false" propagation="REQUIRED"/>
            <tx:method name="delete*" read-only="false"  propagation="REQUIRED"/>
            <tx:method name="update*" read-only="false"  propagation="REQUIRED"/>
            <tx:method name="find*" read-only="true"  propagation="REQUIRED"/>
            <!-- 针对其余的方法 -->
            <tx:method name="*" propagation="NOT_SUPPORTED"/>
            
        </tx:attributes>
   </tx:advice>
   
   <!-- 3AOP配置 -->
   <aop:config>
        <!-- 切入点 -->
        <aop:pointcut expression="execution(* com.qianfeng.service.*.*(..))" id="pc"/>
   
        <!-- 通知 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
   
   </aop:config>
   
</beans>
上一篇下一篇

猜你喜欢

热点阅读