applicationContext.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:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

  http://www.springframework.org/schema/context  

   http://www.springframework.org/schema/context/spring-context-2.5.xsd

">

 

   <!-- 扫描 base-package下的所有类,将带有@Component的类 放入Spring容器 -->

   <context:component-scan  base-package="com.bjsxt"/>

  

   <!-- 使用dbcp 配饰数据库连接池    -->

   <bean  id="ds"  class="org.apache.commons.dbcp.BasicDataSource"   >

        <property name="driverClassName" 

                value="com.mysql.jdbc.Driver"

            </property> 

            <property name="url" value="jdbc:mysql://localhost:3306/power02"></property> 

            <property name="username" value="root"></property> 

            <property name="password" value=""></property>

        <property name="maxActive"  value="20" ></property>

   </bean>

  

   <!-- 配置sessionFactory -->

   <bean  id="sessionFactory"  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >

  

     <!--

     <property name="mappingResources"  >

        <list>

          <value>com/bjsxt/po/User.hbm.xml</value>

        </list>

     </property>

     <property name="annotatedClasses">

        <list>

          <value>com.bjsxt.po.User</value>

        </list>

     </property>

     -->

     <!-- packagesToScan参数会 根据 value下的所有 po的注解,将写映射文件 -->

     <property name="packagesToScan" >

        <list>

          <value>com.bjsxt.po</value>

        </list>

     </property>

    

     <!-- hibernateProperties参数,用来配置hibernate的操作数据的信息 -->

     <property name="hibernateProperties"  >

        <props>

          <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

          <prop key="hibernate.show_sql" >true</prop>

          <prop key="hibernate.format_sql" >true</prop>

          <prop key="hibernate.hbm2ddl.auto" >update</prop>

        </props>

     </property>

    

     <!-- dataSource属性,指向了上面创建的数据库连接池 -->

     <property name="dataSource"  ref="ds"  ></property>

     <!--

         private String[] mappingResources;

         private Properties hibernateProperties;

         private DataSource dataSource;  // 数据源

      -->

      

   </bean>

      

      <!-- 使用模板方法设计模式,配置sessionFacotory值为上面创建的sessionFactory -->

     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" >

        <property name="sessionFactory" ref="sessionFactory"></property>

     </bean>

    

     <!-- 配置事务  sessionFacotry的值为上面创建的sessionFactory-->

     <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >

        <property name="sessionFactory" ref="sessionFactory"></property>

     </bean>

    

     <!--

     对@Transactional这个注解进行的驱动,这是基于注解的方式使用事务配置声明,

     这样在具体应用中可以指定对哪些方法使用事务。

      -->

     <tx:annotation-driven transaction-manager="txManager" />

    

     <!--Spring的声明式事务管理<tx:advice/> 有关的设置  -->

     <tx:advice id="txAdvice" transaction-manager="txManager" >

        <tx:attributes>

          <tx:method name="find*"  read-only="true" propagation="NOT_SUPPORTED"  />

          <tx:method name="add*"  propagation="REQUIRED"  />   

          <tx:method name="del*"  propagation="REQUIRED"/>   

          <tx:method name="update*"  propagation="REQUIRED"/>   

        </tx:attributes>

     </tx:advice>

    

     <!-- 声明一个切面 -->

     <aop:config>

        <aop:pointcut expression="execution(public * com.bjsxt.service.impl.*.*(..))" id="bussinessService"/>

        <aop:advisor advice-ref="txAdvice" pointcut-ref="bussinessService" />

     </aop:config>

 

</beans>

原文地址:https://www.cnblogs.com/liukedong/p/3326547.html