applicationContext-commom.xml配置如下:
<bean id="dataSource" destroy-method="close"
class="com.ssh.util.BasicDataSourceUtil">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:ORCL" />
<property name="username" value="test" />
<property name="password" value="test" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<!-- 有多个值对象的时候添加多个 value节点 -->
<value>com.ssh.pojo.User</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
hibernate.current_session_context_class=thread
</value>
</property>
</bean>
<!-- begin 设置扫描那些包,根据@Repository、@Service、@Configuration、@Component形成对应的bean -->
<context:component-scan base-package="com.ssh.service.impl"/>
<context:component-scan base-package="com.ssh.dao.impl"/>
<context:component-scan base-package="com.ssh.action"/>
<context:component-scan base-package="com.ssh.service.impl.*"/>
<context:component-scan base-package="com.ssh.dao.impl.*"/>
<context:component-scan base-package="com.ssh.action.*"/>
<!-- end -->
<!-- Spring 配置事务 begin -->
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 配置事务传播特性 -->
<tx:advice id="CommonServiceAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="REQUIRED"/>
<tx:method name="get*" propagation="REQUIRED"/>
<tx:method name="confirm*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 配置参与事务的类 -->
<aop:config>
<!-- 定义在service包或者子包里的任意类的任意方法的执行 -->
<aop:pointcut id="allServiceMethod" expression="execution(* com.ssh.service..*.*(..))"/>
<aop:advisor pointcut-ref="allServiceMethod" advice-ref="CommonServiceAdvice" />
</aop:config>
<!-- Spring 配置事务 end -->
1、dataSource配置数据源,此处为oracle本地库;
2、sessionFactory使用AnnotationSessionFactoryBean是使用了hibernate3注解方式;
3、context:component-scan是扫描依赖注入的类;
4、在配置过程中需要注意的几点:
a、配置事务启动的时候出现包冲突引起bean类型错误,经过调整后事务配置时只需要提供2个jar包即可:aspectjweaver.jar、com.springsource.org.aopalliance-1.0.0.jar;
b、正常启动后发现没有进行事务控制,发现是p:pointcut的execution(* com.ssh.service..*.*(..)) 路径地址配置错误,service后面多写一个“.”,表面是service包或者其子包;
c、抛出异常后没有进行回滚操作,本人项目中service代码异常都是如下处理:
catch (DaoException e)
{
e.printStackTrace();
throw new AppException(CommonConstant.COMMONT_FAIL, CommonConstant.COMMONT_FAIL);
}
其中AppException为自己定义的异常类,测试并查看日志文件,发现当抛出该异常的时候数据库将前面正常代码逻辑的业务写入数据库中(若存在),故事务没有生效,
网上查资料发现Spring的回滚默认只能对runtimeException,而本人项目中的AppException只是继承了Exception而没有继承runtimeException,修改方法是将AppExcetion
继承自runtimeException,成功解决问题。(runtimeException是继承Excetion)