beans.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:context="http://www.springframework.org/schema/context"
    xmlns:aop
="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
         http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
>

    
<!-- a service object; we will be profiling its methods -->
    
<context:annotation-config />
    
<context:component-scan base-package="com.bebig" />

    
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method
="close">
        
<property name="driverClassName" value="${jdbc.driverClassName}" />
        
<property name="url" value="${jdbc.url}" />
        
<property name="username" value="${jdbc.username}" />
        
<property name="password" value="${jdbc.password}" />
    
</bean>
    
<context:property-placeholder location="classpath:jdbc.properties" />

    
<bean id="sessionFactory"
        class
="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        
<property name="dataSource" ref="dataSource" />
        
<property name="annotatedClasses">
            
<list>
                
<value>com.bebig.model.User</value>
                
<value>com.bebig.model.Log</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.format_sql">true</prop>
<!--                <prop key="hibernate.current_session_context_class">thread</prop>-->
                
<prop key="hibernate.hbm2ddl.auto">update</prop>
            
</props>
        
</property>
    
</bean>
    
    
<bean id="transactionManager" 
            class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
    
<property name="sessionFactory" ref="sessionFactory"/>
  
</bean>
  
<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>
UserService.java
package com.bebig.service;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bebig.dao.LogDAO;
import com.bebig.dao.UserDAO;
import com.bebig.model.Log;
import com.bebig.model.User;

@Service(
"userService")
public class UserService {
    
private UserDAO userDAO;
    
private LogDAO logDAO;

    
public LogDAO getLogDAO() {
        
return logDAO;
    }


    @Resource
    
public void setLogDAO(LogDAO logDAO) {
        
this.logDAO = logDAO;
    }


    @Transactional
//事务注解
    public void add(User u) {
        userDAO.save(u);
        Log l 
= new Log();
        l.setMsg(
"a user saved!");
        logDAO.save(l);
    }


    @Resource
    
public void setUserDAO(UserDAO userDAO) {
        
this.userDAO = userDAO;
    }


    
public UserDAO getUserDAO() {
        
return userDAO;
    }


    
public void init() {
        System.out.println(
"init.");
    }


    
public void destory() {
        System.out.println(
"destory.");
    }

}