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="packagesToScan">
            
<list>
                
<value>com.bebig.model</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.hbm2ddl.auto">update</prop>
            
</props>
        
</property>
    
</bean>

    
<bean id="transactionManager"
        class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
        
<property name="sessionFactory" ref="sessionFactory" />
    
</bean>

    
<aop:config>
        
<aop:pointcut expression="execution(public * com.bebig.service..*.*(..))"
            id
="servicePointcut" />
        
<aop:advisor advice-ref="txAdvisor" pointcut-ref="servicePointcut" />
    
</aop:config>
    
<tx:advice id="txAdvisor" transaction-manager="transactionManager">
        
<tx:attributes>
            
<tx:method name="getUser" read-only="true" />
            
<tx:method name="add*" propagation="REQUIRED" />
        
</tx:attributes>
    
</tx:advice>
    
    
<!-- 注入hibernateTemplate -->
    
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        
<property name="sessionFactory" ref="sessionFactory" />
    
</bean>

</beans>
LogDAOImpl.java
package com.bebig.dao.impl;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

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

@Repository
public class LogDAOImpl implements LogDAO {
    
//由容器注入hibernateTemplate
    private HibernateTemplate hibernateTemplate;

    
public HibernateTemplate getHibernateTemplate() {
        
return hibernateTemplate;
    }


    @Override
    
public void save(Log log) {
        hibernateTemplate.save(log);
        
// throw new RuntimeException("error!");
    }


    @Resource
    
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        
this.hibernateTemplate = hibernateTemplate;
    }


}

UserDAOImpl.java
package com.bebig.dao.impl;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import com.bebig.dao.UserDAO;
import com.bebig.model.User;

@Repository
public class UserDAOImpl implements UserDAO {
    
//由容器注入hibernateTemplate
    private HibernateTemplate hibernateTemplate;

    @Override
    
public void save(User u) {
        hibernateTemplate.save(u);
    }


    @Resource
    
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        
this.hibernateTemplate = hibernateTemplate;
    }


    
public HibernateTemplate getHibernateTemplate() {
        
return hibernateTemplate;
    }


}