Spring之事务

在将事务之前,先写点别动东西,

1,自动注入

在spring配置文件中,对象属性和需要引入的ref=“id”的id名相同时,不需要使用<property/>标签对其对象名进行赋值,他将会自动赋值。在使用spring整合mybatis中可以使用自动注入,如下配置文件

  <!--  获取数据源-->
      <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      	<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    	<property name="url" value="jdbc:mysql://localhost:3306/yjb"></property>
    	<property name="username" value="root"></property>
    	<property name="password" value="1232626yjb"></property>
      </bean>
      <!-- 获取SqlSession工程 -->
      <bean id="factoryBean" class="org.mybatis.spring.SqlSessionorg.mybatis.spring.SqlSessionFactoryBean">
      	<property name="dataSource" ref="datasource"></property>
      </bean>

可以看到bean对象actoryBean有个对象属性dataSource,和bean对象id为datasource相同,所以可以省略bean对象为actoryBean的<property />,同时添加属性autowire来告诉spring可以使用自动注入,修改成自动注入后的配置文件为:

<!--  获取数据源-->
      <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      	<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    	<property name="url" value="jdbc:mysql://localhost:3306/yjb"></property>
    	<property name="username" value="root"></property>
    	<property name="password" value="1232626yjb"></property>
      </bean>
      <!-- 获取SqlSession工程 -->
      <bean id="factoryBean" class="org.mybatis.spring.SqlSessionorg.mybatis.spring.SqlSessionFactoryBean" autowire=”byName”>
      </bean>
 

1)autowire的可取值有五个,分别为:

default: 默认值,根据全局 default-autowire=””值.默认全局和局部都没有配置情况下,相当于 no

 no: 不自动注

byName: 通过名称自动注入.在 Spring 容器中找类的 Id

byType: 根据类型注入,单数spring 容器中要求不可以出现两个相同类型的<bean>,会注入错误。

 constructor: 根据构造方法注入.,提供对应参数的构造方法(构造方法参数中包含注入对应那个),底层使用 byName, 构造方法参数名和其他<bean>的 id相同。

2)全局配置自动注入的方式为:

<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.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd"
	default-autowire="byName">

就是在beans标签添加一个属性default-autowire。

2,spring加载属性文件。

在很多情况先,我们都会使用软编码建一下配置参数提取到配置文件中,以便部署。例如将连接数据库的四大参数提取出来,连接池的初始化参数提取出来等,以数据库四大参数为例,

第一步:建properties文件,在文件中写入:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/yjb
jdbc.password=1232626y
jdbc.username = root

前缀为jdbc是为了防止和系统变量重名。

在spring配置文件中加载配置文件(多个文件用逗号隔开):

<context:property-placeholder location="classpath:db.properties"/>

获取配置参数格式问

${jdbc.driver}

就可以取出参数值。注意:在使用软编码的时候,spring整合mybatis不能使用SqlSessionFactoryBean而是使用sqlSessionFactoryBeanName。

3,scope 属性

scope属性是用来控制bean是单例还是多例的,bean的默认属性是单例模式,可取值为:

 singleton 默认值,单例

 prototype 多例,每次获取重新实例化

 request 每次请求重新实例化

session 每个会话对象内,对象是单例的.

application 在 application 对象内是单例

global session spring 推 出 的 一 个 对 象 , 依 赖 于 spring-webmvc-portlet ,类似于 session

4,spring配置事务

spring的事务属于声明式事务,事务控制代码已经由 spring 写好.我们只需要声明出哪些方法需要进行事务控制和如何进行事务控制.。声明式事务都是针对于 ServiceImpl 类下方法的,基于通知(advice)实现,

<context:property-placeholder location="classpath:db.properties,classpath:second.properties"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverMa nagerDataSource">

<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property> 
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property> </bean>
<!-- spring-jdbc.jar 中 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSour ceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置声明式事务 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
     <tx:attributes>
<!-- 哪些方法需要有事务控制 -->
<!-- 方法以 ins 开头事务管理 -->
        <tx:method name="ins*" />
        <tx:method name="del*" />
        <tx:method name="upd*" />
        <tx:method name="*" readonly=”boolean”/>
        </tx:attributes>
    </tx:advice>
<aop:config>
<!-- 切点范围设置大一些 -->
<aop:pointcut expression="execution(* com.bjsxt.service.impl.*.*(..))" id="mypoint" />
<aop:advisor advice-ref="txAdvice"pointcut-ref="mypoint" />
</aop:config>

1)readonly=”boolean”,表示只读事务,一般用在查询中使用,

5,spring用于注解

注意:一般情况spring是不使用注解实现,因为管理的类过多,使用注解过于麻烦。

注解扫描

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

表示这个包下的类都可以使用注解。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值