SSH中,spring的配置(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.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx.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">

    <!-- 数据源配置 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">

        <property name="driverClass" value="${c3p0.driverClass}"></property>
        <property name="jdbcUrl" value="${c3p0.url}"></property>
        <property name="user" value="${c3p0.username}"></property>
        <property name="password" value="${c3p0.password}"></property>
        <property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>
        <property name="minPoolSize" value="${c3p0.minPoolSize}"></property>
        <property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property>
        <property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property>
        <property name="acquireIncrement" value="${c3p0.acquireIncrement}"></property>
        <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"></property>
        <property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}"></property>
        <property name="breakAfterAcquireFailure" value="${c3p0.breakAfterAcquireFailure}"></property>
        <property name="maxStatements" value="${c3p0.maxStatements}"></property>
        <property name="testConnectionOnCheckout" value="${c3p0.testConnectionOnCheckout}"></property>
    </bean>
    
    <!-- 加载属性配置文件 -->
    <context:property-placeholder location="classpath:dataSource-configer.properties" />
    
    <!-- 数据源注入sessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="mappingResources">
            <list>
                <value>com/***/www/model/system/***.hbm.xml</value>
                <value>com/***/www/model/system/***.hbm.xml</value>
                <value>com/***/www/model/system/***.hbm.xml</value>
                <value>com/***/www/model/system/***.hbm.xml</value>                
                <value>com/***/www/model/system/***.hbm.xml</value>
                <value>com/***/www/model/***.hbm.xml</value>
                <value>com/***/www/model/***.hbm.xml</value>
                <value>com/***/www/model/***.hbm.xml</value>
                <value>com/***/www/model/***.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>
   
    <!-- 事物管理器配置 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <!-- 事物传播特性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="find*" propagation="REQUIRED" />
            <tx:method name="audit*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
    
        <!-- 参与事物的类 -->
    <aop:config>
        <aop:pointcut expression="execution(public * com.***.www.service..*.*(..))"
            id="pointcutexpression" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcutexpression" />
        <aop:aspect id="logaspect" ref="LogInterceptor">
            <aop:around method="Around" pointcut-ref="pointcutexpression"/>
        </aop:aspect>
    </aop:config>
    
</beans>



方案二

<?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-3.2.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
     http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<!-- 加载链接数据库属性文件 -->
<bean id="joinCDB" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:jdbc.properties</value>
</property>
</bean>

<!-- 配置数据源<DataSource> -->
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.drivermanager}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="url" value="${jdbc.url}"></property>
</bean>

<!-- 声明transactionmanager --> 
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
 <property name="sessionFactory" ref="sessionfactory" />  
  </bean>

<!--创建SessionFactory  -->
<bean id="sessionfactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="datasource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>

<!-- 配置tx:advice --> 
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <!-- readonly 就是read only, 设置操作权限为只读,一般用于查询的方法,优化作用. -->
      <tx:method name="add*" read-only="true"/>
      <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
  </tx:advice>
  
<!--配置aop -->
  <aop:config>
    <aop:pointcut id="serviceOperation" expression=" execution(* com.bochy.dao.impl.*.*(..))"/>
    <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/>
  </aop:config>  
  

<bean name="user" class="com.bochy.bean.Admin"></bean>

<bean name="admindao" class="com.bochy.dao.impl.AdminDaoImpl">
<!-- 在需要使用的sessionfactory类中注入信息 -->
<property name="sf" ref="sessionfactory"></property>
</bean>

<bean name="test" class="com.bochy.test.Test">
<property name="admin" ref="user"></property>
<property name="ad" ref="admindao"></property>
</bean>

</beans>


(备注参考)