spring一些知识

Spring容器配置

1.定义数据源(datasource)
2.通过依赖注入datasource创建sessionFactory
3.通过依赖注入sessionFactory创建hibernateTemplate
4.通过依赖注入hibernateTemplate创建dao
5.通过依赖注入dao创建service
6.通过依赖注入service创建action

配置事务控制
  <!--定义Hibernate的事务管理器HibernateTransactionMana ger-->
    <beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionMana ger">
        <!-- 依赖注入上面定义的sessionFactory-->
        <property name="sessionFactory"ref="sessionFactory"/>
    </bean>

    <!--定义一个事务通知txAdvice,配置事务的传播特性 -->
<tx:advice id="txAdvice"transaction-manager="transactionManager">
<tx:attributes>
<!-- 所有以browse、list、load、get及is开头的业务逻辑方法均不需要事务控制且只读 -->
<tx:method name="browse*"propagation="NOT_SUPPORTED" read-only="true"/>
<tx:method name="list*"propagation="NOT_SUPPORTED" read-only="true"/>
<tx:method name="load*"propagation="NOT_SUPPORTED" read-only="true"/>
<tx:method name="get*"propagation="NOT_SUPPORTED" read-only="true"/>
<tx:method name="is*"propagation="NOT_SUPPORTED" read-only="true"/>
<!-- 设置所有方法均进行事务控制,如果当前没有事务,则新建一个事务-->
<tx:method name="*"propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 基于AOP技术的事务管理实现 -->
<aop:config>
<!--定义一个事务切入点,拦截com.eportal.service.impl包中所有类的所有方法-->
<aop:pointcut id="transactionPointcut"expression="execution(* com.eportal.service.impl.*.*(..))"/>
<!-- 引用txAdvice事务通知 -->
<aop:advisor advice-ref="txAdvice"pointcut-ref="transactionPointcut"/>
</aop:config>

Spring当中常用事务类型
REQUERED:支持当前事务,如果当前没有事务,则新建事务,常用
SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行
MANDATORY:支持当前事务,如果当前没有事务,就抛出异常
REQUIRES-NEW:新建一个事务,如果当前存在事务,则将当前事务挂起
NOT-SUPPORTED:以非事务方式执行,如果当前存在事务,则挂起当前事务
NEVER和NOT-SUPPORTED茶不错,以非事务方式执行,区别在于如果当前存在事务,则抛出异常
NESTED:如果当前存在事务,则嵌套在事务内执行;如果当前没有事务,则执行REQUIRED相同操作。


Table 4.3. Bean scopes

Scope Description
singleton (Default) Scopes a single bean definition to asingle object instance per Spring IoC container.

prototype Scopes a single bean definition to any number ofobject instances.

request Scopes a single bean definition to the lifecycle of asingle HTTP request; that is, each HTTP request has its owninstance of a bean created off the back of a single beandefinition. Only valid in the context of a web-aware SpringApplicationContext.

session Scopes a single bean definition to the lifecycle of anHTTP Session. Only valid in the context of a web-aware SpringApplicationContext.

global session Scopes a single bean definition to thelifecycle of a global HTTP Session. Typically only valid when usedin a portlet context. Only valid in the context of a web-awareSpring ApplicationContext.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值