spring 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

<beans> 

1。复杂的形式

<!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="mappingResources">
            <list>
                <value>com/zsoft/strutsdemo/model/Person.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
            <prop key="hibernate.show_sql">true</prop>

            <prop key="hibernate.connection.driver_class">com.jnetdirect.jsql.JSQLDriver</prop>
            <prop key="hibernate.connection.url">jdbc:JSQLConnect://srcctrl/mvc</prop>
            <prop key="hibernate.connection.username">sa</prop>
            <prop key="hibernate.connection.password"></prop>
            <prop key="jdbc.use_scrollable_resultset">true</prop>
            <prop key="hibernate.query.substitutions">true 1, false 0, yes 'Y', no 'N'</prop>
            <prop key="hibernate.connection.pool.size">20</prop>
            <prop key="jdbc.fetch_size">50</prop>
            <prop key="jdbc.batch_size">25</prop>
            <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>

        </props>
        </property> 
    </bean> 

2 简化的形式,hibernate属性由外部的hibernate.properties提供

<bean id="cms.SessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="lobHandler">   处理oracle clob,blob时需要
   <ref bean="oracleLobHandler" />
  </property>
  <property name="configLocation"
   value="/cms/cms-hibernate.cfg.xml" />
 </bean>

<!-- use spring+hibernate handle oracle BLOB-->
 <bean id="nativeJdbcExtractor"
  class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor" />
 <bean id="oracleLobHandler"
  class="org.springframework.jdbc.support.lob.OracleLobHandler"
  lazy-init="true">
  <property name="nativeJdbcExtractor">
   <ref bean="nativeJdbcExtractor" />
  </property>
 </bean>

 

事务管理的配置

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" />
 <!-- using HibernateTransactionManager for local test
 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
-->

   <property name="sessionFactory" ref="visit.SessionFactory"/>
  </bean>

事务拦截器配置

<bean id="transactionInterceptor"
      class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager" ref="transactionManager"/>
  <property name="transactionAttributes">
        <props>

            <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
            <prop key="*">PROPAGATION_REQUIRED</prop>
        </props>
    </property>
  </bean>

<bean id="transactionAttributeSource"    不知道有什么用途?
      class="org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource">
      <property name="transactionAttribute"><value>PROPAGATION_REQUIRED</value></property>
  </bean>

事务代理对象:获取service时,应指定这里定义的bean id,才能使用spring事务管理

1,使用AOP,进行代理,需要指定拦截器,管理事务

<bean id="visit.ActionProcessor" class="org.springframework.aop.framework.ProxyFactoryBean">
  <property name="proxyInterfaces">
      <value>wegov.visit.service.ActionProcessor</value>
  </property>
  <property name="interceptorNames">
      <list>
        <value>transactionInterceptor</value>
        <value>visit.ActionProcessorTarget</value>
      </list>
   </property>
</bean>

2,不需要额外的配置,就可以支持事务了
<bean id="cms.RepositoryManager"
  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager" ref="transactionManager" />
  <property name="target" ref="RepositoryManager" />
  <property name="transactionAttributes">
   <props>
    <prop key="*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>

3,模版形式,bean通过parent属性使用事务模版

 <bean id="txProxyTemplate" abstract="true"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager"><ref bean="transactionManager"/></property>
    <property name="transactionAttributes">
        <props>
            <prop key="save*">PROPAGATION_REQUIRED</prop>
            <prop key="update*">PROPAGATION_REQUIRED</prop>
            <prop key="delete*">PROPAGATION_REQUIRED</prop>
            <prop key="remove*">PROPAGATION_REQUIRED</prop>
            <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
        </props>
    </property>
  </bean>

  <bean id="personManager" parent="txProxyTemplate">
        <property name="target">
            <bean class="com.zsoft.strutsdemo.service.impl.PersonManagerImpl"></bean>
        </property>
  </bean>

4,autowire形式,要指定事务拦截器

业务bean要声明autowire属性

<bean autowire="byName" id="contact.ContactRelationHelper" class="wegov.contact.service.ContactRelationHelper">
  <property name="_relationDao" ref="contact.RelationDao"/>
 </bean>

<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <property name="beanNames">
   <value>*Helper</value>
  </property>
  <property name="interceptorNames">
   <list>
    <value>transactionInterceptor</value>
    <!-- add new Interceptor -->
   </list>
  </property>
 </bean>

 <bean 不知道这个bean是不是必须的?class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
  <property name="transactionInterceptor" ref="transactionInterceptor" />
 </bean>

业务bean声明

1,形式一

<bean id="visit.ProjectDao" class="wegov.visit.persistent.ProjectDao">
    <constructor-arg>
     <ref bean="visit.SessionFactory"/> 有单参数的构造函数
   </constructor-arg>

2,形式二:多参数的构造

<bean id="visit.WeTask" class="com.wondersgroup.foundation.wetask.WeTask">
   <constructor-arg index="0"><ref bean="visit.SessionFactory"/></constructor-arg>
   <constructor-arg index="1"><value>wegov.visit.common.VisitTask</value></constructor-arg>
   <constructor-arg index="2"><value>com.wondersgroup.foundation.wetask.WeTaskRightInfo</value></constructor-arg>
  </bean>

3,IoC注入

<bean id="visit.WorkFlowHelper" class="wegov.visit.service.WorkFlowHelper"  init-method="init" singleton="false">
    <property name="taskMng" ref="visit.WeTask"/>
    <property name="rightHelper" ref="visit.RightHelper"/>
  </bean>

</beans>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值