Spring学习笔记(3)

来源:互联网 发布:消原唱软件 编辑:程序博客网 时间:2024/05/16 17:21

1.Spring AOP框架相关概念

Aspect(切面): 是通知和切入点的结合,通知和切入点共同定义了关于切面的全部内容---它的功能、在何时和何地完成其功能。

 

joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点。如personService的add方法之前。

 

Pointcut(切入点): 指一堆连接点的集合。比如所有名字为add*的方法之前。有一种表达式语言描述这个切入点。Spring使用AspectJ的切入点描述语法,有点类似于正则表达式。execution(*cn.itcast.gz.service..add*(..))。

 

Advice(通知): 指在连接点(切入点)的什么位置,做什么事情。

 

Target(目标对象):代理的目标对象。即对哪个对象进行切入其它方面的代码。

 

Weaving(织入): 把几个切面的代码切入核心业务组件的过程。静态织入,在编译的时候就织入(支持aop特殊编译器,AspectJ)。动态织入,编译使用的普通的java编译器,在运行的时候通过代理来进行织入(Spring)。静态织入比动态织入运行性能高。

 

Introduction(引入): 在不修改代码的情况下,给一个已经存在的业务组件动态添加一些方法,字段,增加功能。(动态让其去实现一些接口。)

2.Spring AOP框架的基本应用

2.1 AOP简单应用

1)引入aop相关的jar包

aopalliance.jar aop         联盟

aspectjweaver.jar          AspectJ

cglib-nodep-2.1_3.jar           生成代理类

org.springframework.aop-3.1.1.RELEASE.jar aop     核心包

 

2)在beans.xml配置文件中加入aop命名空间

3)配置切面Bean

4)配置切面

5)配置切入点

6)配置通知

配置文件:

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

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

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

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

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

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

      <bean id="personDAO"class="com.maple.spring.aop.PersonDAOImpl"/>

       <!-- 配置切面Bean -->

      <bean id="securityAspectBean"class="com.maple.spring.aop.SecurityAspect"/>

      <!-- aop配置 -->

      <aop:config>

          <!-- 配置切面,切面会和一个切面Bean关联 -->

          <aop:aspectref="securityAspectBean">

                <!-- 配置切入点 -->

                <aop:pointcutexpression="execution(*com.maple.spring.aop.IPersonDAO.*(..))"id="personDAOMethods"/>

                <!-- 配置通知 在切入点的什么位置做什么事情(执行切面的checkPermission方法) pointcut-ref用来指定在什么切入点下执行通知-->

<aop:beforemethod="checkPermission"pointcut-ref="personDAOMethods"/>

          </aop:aspect>

      </aop:config>

</beans>

切面Bean:

publicclass SecurityAspect {

      publicboolean checkPermission(){//无参数的方法

          System.out.println("执行权限检查....----------");

          boolean flag =false;

          if(!"admin".equals(UserContext.getUser())){

                System.out.println("无权限。。。--------");

                thrownew RuntimeException("没有权限!");

          }else {

                flag= true;

                System.out.println("有权限。。。--------");

          }

          return flag;

      }

}

全局切入点的配置:

但有多个切面要织入一个同一个切入点的时候,可以将切入点配置成全局的切入点,即将其提取到切面外面。

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

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

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

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

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

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

      <bean id="personDAO"class="com.maple.spring.aop.PersonDAOImpl"/>

       <!-- 配置切面Bean -->

      <bean id="securityAspectBean"class="com.maple.spring.aop.SecurityAspect"/>

      <bean id="writeLogAspectBean"class="com.maple.spring.aop.WriteLogAspect"/>

      <!-- aop配置 -->

      <aop:config>

          <!-- 配置一个全局切入点 -->

<aop:pointcutexpression="execution(*com.maple.spring.aop.IPersonDAO.*(..))"id="personDAOMethods"/>

          <!-- 配置权限检查切面,切面会和一个切面Bean关联 -->

          <aop:aspectref="securityAspectBean">

                <!-- 配置通知 在切入点的什么位置做什么事情(执行切面的checkPermission方法) pointcut-ref用来指定在什么切入点下执行通知-->

                <aop:beforemethod="checkPermission"pointcut-ref="personDAOMethods"/>

          </aop:aspect>

          <!-- 配置日志记录切面 -->

          <aop:aspectref="writeLogAspectBean">

                <aop:beforemethod="writeLogBefore" pointcut-ref="personDAOMethods"/>

                <aop:after-returningmethod="writeLogAfter" pointcut-ref="personDAOMethods"/>

          </aop:aspect>

      </aop:config>

</beans>

2.2AOP通知详解—基于XML配置

1)前置通知 aop:before

2)后置通知 aop:after-running

3)异常通知 aop:after-throwing

4)最终通知 aop:after

5) 环绕通知(可以替代前面四种通知) aop:around

<beanid="personDAO"class="com.maple.spring.aop.PersonDAOImpl"/>

      <bean id="writeLogAspectBean"class="com.maple.spring.aop.WriteLogAspect"/>

<aop:config>

<aop:pointcutexpression="execution(*com.maple.spring.aop.IPersonDAO.*(..))"id="personDAOMethods"/>

          <aop:aspectref="writeLogAspectBean">

<!-- 前置通知 用aop:before配置,在切入点方法执行之前就执行 -->

<aop:beforemethod="writeLogBefore" pointcut-ref="personDAOMethods"/>

<!-- 后置通知 用aop:after-returning配置,在切入点方法成功执行并返回后才执行 -->

                <aop:after-returningmethod="writeLogAfter" pointcut-ref="personDAOMethods"/>

<!-- 异常通知 用aop:after-throwing配置,在切入点的方法出现异常时执行,切面Bean中的方法需要一个Exeception参数throwing:值要和method指定的方法的参数名一致 -->

<aop:after-throwingmethod="wrtetLogThrow" pointcut-ref="personDAOMethods"throwing="e"/>

<!-- 最终通知 用aop:after配置,不管切入点的方法是否成功执行都会执行。类似try catch中的finally -->

<aop:aftermethod="wrtetLogFianl"pointcut-ref="personDAOMethods"/></aop:aspect>

      </aop:config>

5) 环绕通知(可以替代前面四种通知) aop:around

 

所谓的环绕通知是指环绕在切入点四周的方法。用环绕通知可以在切入点方法的前面后面,任意位置执行执行自己的代码,由自己控制要不要调用目标对象的方法。

使用环绕通知时,对切面Bean中的方法是有要求的,方法必须返回Object类,并有一个ProceedingJoinPoint类型的参数。如:

public Object check(ProceedingJoinPointpoint) {

       .....

       Objectret = ....

       returnret

}

切面Bean:

publicclass AroundAspect {

      /**

       * 方法必须返回Object类型且有一个ProceedingJoinPoint类型的参数

       */

      public Objectcheck(ProceedingJoinPoint point) {

          Objectobj = null;

          try {

                //前置通知

                System.out.println("执行权限检查...........");

                 obj = point.proceed();//放行,即执行目标对象上的方法

                //后置通知

                System.out.println("执行审计功能...........");

          }catch (Throwable e) {

                //异常通知

                System.out.println("出现异常" + e.getMessage());

                thrownew RuntimeException(e.getMessage());

          }finally {

                //最终通知

                System.out.println("方法调用完了");

          }

          return obj;

      }

}

配置文件:

<beanid="personDAO"class="com.maple.spring.aop.PersonDAOImpl"/>

      <bean id="aroundAspectBean"class="com.maple.spring.aop.AroundAspect"/>

      <aop:config>

          <aop:pointcutexpression="execution(*com.maple.spring.aop.IPersonDAO.*(..))"id="personDAOMethods"/>

          <aop:aspectref="aroundAspectBean">

                <!-- 环绕通知 用aop:around配置 -->

<aop:aroundmethod="check"pointcut-ref="personDAOMethods"/>

          </aop:aspect>

      </aop:config>

2.3AOP通知详解—基于Annotation配置

1)用到的注解

 

@Aspect 用在类上,说明该类是一个切面Bean。

@Pointcut("execution(*com.maple.spring..*(..))")用在自定义方法上,定义一个切入点

@Before("personDAOMehtods()")前置通知

@AfterReturning("personDAOMehtods()")后置通知

@AfterThrowing(pointcut="personDAOMehtods()",throwing="e")@After("personDAOMehtods()")最终通知

@Around("personDAOMehtods()")环绕通知

@Aspect//说明该类是一个切面Bean

publicclassWriteLogAspectAnntation {

      //配置一个名为personDAOMehtods()的切入点

      @Pointcut("execution(*com.maple.spring..*(..))")

      publicvoidpersonDAOMehtods(){  }

      @Before("personDAOMehtods()")

      publicvoid writeLogBefore() {

          System.out.println(UserContext.getUser()+"准备调用方法");

      }

      @AfterReturning("personDAOMehtods()")

      publicvoid writeLogAfter() {

          System.out.println(UserContext.getUser()+"调用方法成功");

      }

      @AfterThrowing(pointcut="personDAOMehtods()", throwing="e")

      publicvoidwrtetLogThrow(Exception e) {

          System.out.println(UserContext.getUser()+"出现异常:" +e.getMessage() +new Date());

      }    

      @After("personDAOMehtods()")

      publicvoid wrtetLogFianl() {

System.out.println(UserContext.getUser()+"最终。。。" + new Date());

      }

}

3. Spring中的数据库

3.1模版技术

把具有相同功能的代码模板抽取到一个工具类中。如把访问jdbc的模板代码抽到Template中,使用模板类,可以不用管有关连接管理,关闭等细节。只在我们的代码包括核心业务的代码把访问jdbc的模板代码抽到Template中,使用模板类,可以不用管有关连接管理,关闭等细节。只在我们的代码包括核心业务的代码。

3.2 Spring中的JdbcTemplete

Spring 提供了JdbcTemplate类,用于简化jdbc操作。只要写核心业务代码就可以了。

publicclass PersonImplimplements IPersonDAO {

      //使用Sprig提供的jdbc操作模版

     private JdbcTemplatetemplate;

      /**

       * 提供setter方法,让spring注入值

       * @param template

       */

      publicvoid setTemplate(JdbcTemplatetemplate) {

          this.template = template;

      }

      @Override

      publicvoid save(final Person p) {

          //调用自定的executeUpdate方法进行保存

          Stringsql = "insert into persons(name, age) values('"+ p.getName() +"', " + p.getAge()+")";

          template.execute(sql);

      }

      @Override

      publicvoid remove(final Long id) {

          Stringsql = "delete from persons where id=" + id;

          template.execute(sql);

      }

      @Override

      publicvoid update(final Long id,final Person p) {

          Stringsql = "update persons set name='" + p.getName() +"', age=" + p.getAge() +" where id =" + id;

          template.execute(sql);

      }

}

配置文件:

      <!-- 加载数据库连接配置文件 -->

      <context:property-placeholderlocation="db.properties"/>

      <!-- 定义数据源,直接从连接池中获取 -->

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

          <!-- 创建数据源时需要四个参数 -->

<propertyname="driverClassName"value="${driverClassName}"/>

<propertyname="url"value="${url}"/>

<propertyname="username"value="${username}"/>

<propertyname="password"value="${password}"/>

      </bean>

      <!-- 定义template 需要注入一个dataSource给构造函数-->

      <bean id="template"class="org.springframework.jdbc.core.JdbcTemplate">

          <constructor-argname="dataSource"ref="dataSource"/>

      </bean>

      <bean id="personDAO"class="com.maple.spring.dao.impl.PersonImpl">

          <!-- 注入template属性 -->

          <propertyname="template"ref="template"/>

      </bean>

3.3使用JdbcTemplete最好的方式

以上方式使用Spring提供的JdbcTemplete还需要手动注入。不是最好的方法。最好的是让实现类同时继承JdbcDAOSupport类,该类提供了getJdbcTemplate()方法,可以直接获取JdbcTemplate对象。

publicclass PersonImpl2extends JdbcDaoSupportimplements IPersonDAO {

      @Override

      publicvoid save(final Person p) {

          Stringsql = "insert into persons(name, age) values('"+ p.getName() +"', " + p.getAge()+")";

          getJdbcTemplate().execute(sql);

      }

      @Override

      publicvoid remove(final Long id) {

          Stringsql = "delete from persons where id=" + id;

          getJdbcTemplate().execute(sql);

      }

      @Override

      publicvoid update(final Long id,final Person p) {

          Stringsql = "update persons set name='" + p.getName() +"', age=" + p.getAge() +" where id =" + id;

          getJdbcTemplate().execute(sql);

      }

 

      @Override

      public Person get(final Long id) {

          //要传入一个RowMapper对象,自己处理返回结果集

          returngetJdbcTemplate().queryForObject("select * from persons where id=" + id,new MyRowMapper());

      }

      @Override

      public List<Person>list() {

returngetJdbcTemplate().query("select * from persons",new MyRowMapper());

      }

      /**

       * TODO自定义RowMapper

       */

      privateclass MyRowMapperimplementsRowMapper<Person> {

          @Override

public PersonmapRow(ResultSet rs,int rowNum)throws SQLException {

                Personp  = new Person();

                p.setId(rs.getLong("id"));

                p.setName(rs.getString("name"));

                p.setAge(rs.getInt("age"));

                return p;

          }

      }

}

配置文件:

<!-- 加载数据库连接配置文件 -->

      <context:property-placeholderlocation="db.properties"/>

      <!-- 定义数据源,直接从连接池中获取 -->

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

          <!-- 创建数据源时需要四个参数 -->

<propertyname="driverClassName"value="${driverClassName}"/>

          <propertyname="url"value="${url}"/>

          <propertyname="username"value="${username}"/>

          <propertyname="password"value="${password}"/>

      </bean>

      <bean id="personDAO"class="com.maple.spring.dao.impl.PersonImpl2">

          <!-- 因为PersonImpl继承了JdbcDaoSupport类,而它初始化时需要一个dataSource -->

          <propertyname="dataSource"ref="dataSource"/>

      </bean>

4.事务管理

4.1事务概述

事务是一组操作的执行单元,相对于数据库操作来讲,事务管理的是一组SQL指令,比如增加,修改,删除等,事务的一致性,要求,这个事务内的操作必须全部执行成功,如果在此过程种出现了差错,比如有一条SQL语句没有执行成功,那么这一组操作都将全部回滚。

 

事务的特点(ACID

Atomic(原子性):要么都发生,要么都不发生。

Consistent(一致性):数据应该不被破坏。

Isolate(隔离性):用户间操作不相混淆。

Durable(持久性):永久保存,例如保存到数据库中等。

 

事务隔离级别:

DEFAULT     使用后端数据库默认的隔离级别(spring中的的选择项)。

READ_UNCOMMITED      允许你读取还未提交的改变了的数据。可能导致脏、幻、不可重复读。

READ_COMMITTED 允许在并发事务已经提交后读取。可防止脏读,但幻读和不可重复读仍可发生。

REPEATABLE_READ 对相同字段的多次读取是一致的,除非数据被事务本身改变。可防止脏、不可重复读,但幻读仍可能发生。

SERIALIZABLE   完全服从ACID的隔离级别,确保不发生脏、幻、不可重复读。这在所有的隔离级别中是最慢的,它是典型的通过完全锁定在事务中涉及的数据表来完成的。

 

脏读:一个事务读取了另一个事务改写但还未提交的数据,如果这些数据被回滚,则读到的数据是无效的。

不可重复读:在同一事务中,多次读取同一数据返回的结果有所不同。换句话说就是,后续读取可以读到另一事务已提交的更新数据。相反,“可重复读”在同一事务中多次读取数据时,能够保证所读数据一样,也就是,后续读取不能读到另一事务已提交的更新数据。

幻读:一个事务读取了几行记录后,另一个事务插入一些记录,幻读就发生了。再后来的查询中,第一个事务就会发现有些原来没有的记录。

 

小结:

不同的隔离级别采用不同的锁类型来实现,在四种隔离级别中,Serializable的隔离级别最高,Read Uncommited的隔离级别最低。

 

大多数据库默认的隔离级别为ReadCommited,如SqlServer,也有少部分数据库默认的隔离级别为Repeatable_Read ,如Mysql。

 

Oracle数据库支持READ COMMITTED和SERIALIZABLE两种事务隔离性级别,不支持READ UNCOMMITTED和REPEATABLE READ这两种隔离性级别。虽然SQL标准定义的默认事务隔离性级别是SERIALIZABLE,但是Oracle数据库默认使用的事务隔离性级别却是READ COMMITTED.

4.2 Spring管理事务的方法

1)编程序事务管理

编写程序式的事务管理可以清楚的定义事务的边界,可以实现细粒度的事务控。比如可以通过程序代码来控制你的事务何时开始,何时结束等,与声明式事务管理相比,它可以实现细粒度的事务控制。

 

2)声明式事务管理

如果程序不需要细粒度的事务控制,则可以使用声明式事务,在Spring中,只需要在Spring配置文件中做一些配置,即可将操作纳入到事务管理中,解除了和代码的耦合, 这是对应用代码影响最小的选择,从这一点再次验证了Spring关于AOP的概念。当程序不需要事务管理的时候,可以直接从Spring配置文件中移除该设置。

4.3基于XML配置的申明式事务

步骤:

1.      在配置文件中引入用于声明事务的tx命名空间。

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

xsi:schemaLocation="http://www.springframework.org/schema/tx

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

2.      配置数据源

3.      配置dao和service Bean

4.      配置事务管理器DataSourceTransactionManager并注入其依赖的数据源

5.      配置事务切入点(配置哪些方法需要事务tx:method)

6.      配置切面并加入增强点(aop:config,aop:advisor)

 

 

propagation的取值:

REQUIRED(常用)业务方法需要在一个事务中运行。如果方法运行时,已经处在一个事务中,那么加入到该事务,否则为自己创建一个新的事务。

 

SUPPORTS(常用)      这一事务属性表明,如果业务方法在某个事务范围内被调用,则方法成为该事务的一部分。如果业务方法在事务范围外被调用,则方法在没有事务的环境下执行。

 

NOT_SUPPORTED     声明方法不需要事务。如果方法没有关联到一个事务,容器不会为它开启事务。如果方法在一个事务中被调用,该事务会被挂起,在方法调用结束后,原先的事务便会恢复执行。

 

REQUIRESNEW  属性表明不管是否存在事务,业务方法总会为自己发起一个新的事务。如果方法已经运行在一个事务中,则原有事务会被挂起,新的事务会被创建,直到方法执行结束,新事务才算结束,原先的事务才会恢复执行。

 

MANDATORY     该属性指定业务方法只能在一个已经存在的事务中执行,业务方法不能发起自己的事务。如果业务方法在没有事务的环境下调用,容器就会抛出例外。

 

Never     指定业务方法绝对不能在事务范围内执行。如果业务方法在某个事务中执行,容器会抛出例外,只有业务方法没有关联到任何事务,才能正常执行。

 

NESTED       如果一个活动的事务存在,则运行在一个嵌套的事务中. 如果没有活动事务, 则按REQUIRED属性执行.它使用了一个单独的事务,这个事务拥有多个可以回滚的保存点。内部事务的回滚不会对外部事务造成影响。它只对DataSourceTransactionManager事务管理器起效。

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

      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:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

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

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

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

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

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

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

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

      <!-- 加载数据库连接配置文件 -->

      <context:property-placeholderlocation="db.properties"/>

      <!-- 定义数据源,直接从连接池中获取 -->

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

          <!-- 创建数据源时需要四个参数 -->

<propertyname="driverClassName"value="${driverClassName}"/>

          <propertyname="url"value="${url}"/>

          <propertyname="username"value="${username}"/>

          <propertyname="password"value="${password}"/>

      </bean>

      <!-- 配置accountDAO -->

      <bean id="accountDAO"class="com.maple.spring.dao.impl.AccoutDAOImpl">

<propertyname="dataSource"ref="dataSource"/>

      </bean>

      <!-- 配置TransferServiceImpl -->

      <bean id="transferService"class="com.maple.spring.service.impl.TransferServiceImpl">

          <!-- 注入所依赖的dao -->

          <propertyname="accountDAO"ref="accountDAO"/>

      </bean>

      <!-- 配置事务管理器DataSourceTransactionManager是PlatformTransactionManager的实现类,专用于数据源的事务管理

      依赖于数据源 -->

      <bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

          <propertyname="dataSource" ref="dataSource"/>

      </bean>

      <!-- 配置事务通知(切入点),即在什么地方需要事务,需要依赖一个事务管理器PlatformTransactionManager -->

      <tx:advice id="transferServiceAdvice"transaction-manager="transactionManager">

          <!-- 配置事务属性 -->

          <tx:attributes>

<!-- tx:method用于配置哪些方法需要事务propagation用于指定是需要事务的 。REQUIRED说明执行时必须有事务,如果没有事务则创建一个新的事务,有就加入事务中执行-->

<tx:methodname="saveMoney"propagation="REQUIRED"/>

<tx:methodname="takeMoney"propagation="REQUIRED"/>

<tx:methodname="transferMoney"propagation="REQUIRED"/>

<!-- *用于用于指定除以上三个方法的其他方法支持事务,SUPPORTS表示有事务加入事务中执行,无也不会创建新的事务 -->

<tx:methodname="*"propagation="SUPPORTS"/>

          </tx:attributes>

      </tx:advice>

      <!-- 配置切面,要加入aop命名空间 -->

      <aop:config>

          <!-- 配置一个增强切入点 -->

<aop:advisoradvice-ref="transferServiceAdvice"pointcut="execution(*com.maple.spring.service.impl.TransferServiceImpl.*(..))"/>

      </aop:config>

</beans>

4.4基于注解配置的申明式事务

用到的注解:

@Transactional 该注解可以用于类或方法上,用于指明该类的所有方法或某个方法需要事务。

<!-- 指明事务在注解中配置,让框架自动扫描 -->

<tx:annotation-driven/>

 

@Transactional//该类的所有方法都需要事务

publicclassTransferServiceImplAnnoimplements ITransferService {

      @Override

      publicvoid saveMoney(Stringname, Double money) {

          //先判断账户是否存在

          Accounta = accountDAO.get(name);

          if(a !=null) {

                a.setBalance(a.getBalance()+ money);

                accountDAO.update(a.getId(),a);//更新账户的钱

          }else {

                //否则新创建一个账户并将钱存进去

                a= new Account(name, money);

                accountDAO.save(a);

          }

      }

/**

       * 给方法添加额外的事务配置,在方法中配置的事务优先执行

       */

@Transactional(propagation=Propagation.SUPPORTS,readOnly=true)

      public AccountgetAccount(String name){

          returnaccountDao.get(name);

      }

}

配置文件:

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

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

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

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

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

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

 

      <!-- 引入数据库配置文件 -->

      <import resource="beans-db.xml"/>

      <!-- 配置accountDAO -->

      <bean id="accountDAO"class="com.maple.spring.dao.impl.AccoutDAOImpl">

          <propertyname="dataSource"ref="dataSource"/>

      </bean>

      <!-- 配置TransferServiceImpl -->

      <bean id="transferService"class="com.maple.spring.service.impl.TransferServiceImplAnno">

          <!-- 注入所依赖的dao -->

          <propertyname="accountDAO"ref="accountDAO"/>

      </bean>

      <!-- 配置事务管理器DataSourceTransactionManager是PlatformTransactionManager的实现类,专用于数据源的事务管理

      依赖于数据源 -->

      <bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

          <propertyname="dataSource" ref="dataSource"/>

      </bean>

      <!-- 指明事务在注解中配置,让框架自动扫描 -->

     <tx:annotation-driven />

</beans>

 

原创粉丝点击