<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- Properties文件加载器 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>
classpath:/spring_hibernate/config/db/jdbc.properties
</value>
</property>
</bean> <!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean> <!-- 注册sessionfactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/spring_hibernate/config/hibernate/</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.hbm2ddl.auto">update</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
</bean> <!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"></ref>
</property>
</bean> <!-- 注册Hibernate模版对象 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>

<!-- spring事务拦截器 -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
     <!-- key 方法名      value 策略-->
     <!-- 传播行为   隔离级别  异常处理-->
     <prop key="add*">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,-Throwable</prop>
     <prop key="delete*">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,-Throwable</prop>
     <prop key="update*">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,-Throwable</prop>
     <prop key="search*">readOnly</prop>
     </props>
</property>
</bean>

<bean id="autoTransactionProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
     <property name="beanNames">
     <list>
     <value>*ServiceImpl</value>
     </list>
     </property>
     <property name="interceptorNames">
     <list>
     <value>transactionInterceptor</value>
     </list>
     </property>
    </bean> <bean id="messageDao" class="spring_hibernate.dao.impl.MessageDaoImpl">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean> <bean id="messageService" class="spring_hibernate.service.impl.MessageServiceImpl">
<property name="messageDao">
<ref bean="messageDao"/>
</property>
</bean>
<!--  
BeanPostProcessor类型对象可以用bean注册方式或者完全配置的方式
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"></bean>
<context:annotation-config/> 
-->
</beans>
下面是Dao实现类代码:
package spring_hibernate.dao.impl;import java.util.List;import org.hibernate.LockMode;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.HibernateTemplate;import spring_hibernate.dao.IMessageDao;
import spring_hibernate.entity.Message;public class MessageDaoImpl implements IMessageDao{
private HibernateTemplate hibernateTemplate;

public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
} public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
} public void addMessage(Message message) throws RuntimeException{
hibernateTemplate.save(message);
int a = 10 /0;
hibernateTemplate.save(message);
}

public void deleteMessage(Long id) {
hibernateTemplate.delete(searchMessageById(id), LockMode.NONE);
}

public List<Message> searchAllMessage() {
return hibernateTemplate.findByCriteria(DetachedCriteria.forClass(Message.class));
}

public Message searchMessageById(Long id) {
return hibernateTemplate.load(Message.class, id);
}

public void updateMessage(Message message) {
hibernateTemplate.update(message);
}
}下面是service实现类代码:
public class MessageServiceImpl implements IMessageService{
private IMessageDao messageDao;
public IMessageDao getMessageDao() {
return messageDao;
}
public void setMessageDao(IMessageDao messageDao) {
this.messageDao = messageDao;
}
@Override
public void addMessage(Message message) {
messageDao.addMessage(message);

}
下面是测试类代码:
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring_hibernate/config/spring/applicationcontext.xml");
IMessageService messageService = (MessageServiceImpl)applicationContext.getBean("messageService");
System.out.println(applicationContext.getBean("messageService").getClass());
Message message = new Message("测试标题","测试作者","神马都是浮云!",new Date());
messageService.addMessage(message);
}
}
我在dao层人为导致代码异常,但是事务还是提交了,请高手解答,为什么事务不起作用,很郁闷!

解决方案 »

  1.   

    这段代码中可以看出来你的proxy中是针对*ServiceImpl的bean进行代理的,你可以尝试写*Service,,bean名称都要以*Service格式定义
      

  2.   

    根据你的建议,我把MessageServiceImpl来更名为MessageService,applicationcontext.xml中配置更改如下:<bean id="autoTransactionProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
         <property name="beanNames">
         <list>
         <value>*Service</value>
         </list>
         </property>
         <property name="interceptorNames">
         <list>
         <value>transactionInterceptor</value>
         </list>
         </property>
        </bean>更改后,运行数据没有插入数据库,但是抛异常,不是我人为抛出的异常,貌似是类型转换异常,请指教!
    Exception in thread "main" java.lang.ClassCastException: $Proxy0 cannot be cast to spring_hibernate.service.impl.MessageService
    at spring_hibernate.test.Test.main(Test.java:15)
      

  3.   

    spring配置文件改成:<bean id="autoTransactionProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
            <property name="beanNames">
                <list>
                    <value>MessageService</value>
                </list>
            </property>
            <property name="interceptorNames">
                <list>
                    <value>transactionInterceptor</value>
                </list>
            </property>
        </bean>
    后,运行,数据仍然还是插进去了,事务没回滚,郁闷,还望大家多多指教!郁闷2天了,我感激不尽!
    [acgm] 2012-01-12 00:14:17 WARN [main] DTDEntityResolver.resolveEntity(73) | recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
    [acgm] 2012-01-12 00:14:18 INFO [main] HibernateTransactionManager.afterPropertiesSet(415) | Using DataSource [org.apache.commons.dbcp.BasicDataSource@136d9d8] of Hibernate SessionFactory for HibernateTransactionManager
    class spring_hibernate.service.impl.MessageService
    [acgm] 2012-01-12 00:14:18 DEBUG [main] SQL.logStatement(111) | insert into message (title, author, content, createTime) values (?, ?, ?, ?)
    Hibernate: insert into message (title, author, content, createTime) values (?, ?, ?, ?)
    Exception in thread "main" java.lang.ArithmeticException: / by zero
    at spring_hibernate.dao.impl.MessageDaoImpl.addMessage(MessageDaoImpl.java:25)
    at spring_hibernate.service.impl.MessageService.addMessage(MessageService.java:23)
    at spring_hibernate.test.Test.main(Test.java:18)
      

  4.   

    经过我自己的不懈努力,还是我自己解决了,哈哈!谢谢loveliy520的回复!