Spring -- spring结合aop 进行 tx&aspectj事务管理配置方法

1. tx 配置方法, 代码示例

javabean及其映射文件省略,和上篇的一样

CustomerDao.java, dao层接口

public interface CustomerDao {
	public void insertCustomer(Customer c);
	public void updateCustomer(Customer c);
	public List<Customer> findCustomerByName(String name);
	
	//批量保存
	//public void saveCustomers(List<Customer> list);
}

CustomerDaoImpl.java ,dao层实现

public class CustomerDaoImpl implements CustomerDao {

	private SessionFactory sf ;
	public void setSf(SessionFactory sf) {
		this.sf = sf;
	}

	public List<Customer> findCustomerByName(String name) {
		String hql = "from Customer c where c.name = ?";
		Query q = sf.getCurrentSession().createQuery(hql);
		q.setString(0, name);
		return q.list();
	}

	public void insertCustomer(Customer c) {
		sf.getCurrentSession().save(c);
	}

	public void updateCustomer(Customer c) {
		sf.getCurrentSession().update(c);
	}
}

CustomerService.java ,service层接口

public interface CustomerService {
	public void insertCustomer(Customer c);
	public void updateCustomer(Customer c);
	public List<Customer> findCustomerByName(String name);
	
	//批量保存
	public void saveCustomers(List<Customer> list);
}

CustomerServiceImpl.java,service层实现

public class CustomerServiceImpl implements CustomerService {
	//dao
	private CustomerDao dao ;

	//注入dao
	public void setDao(CustomerDao dao) {
		this.dao = dao;
	}

	public List<Customer> findCustomerByName(String name) {
		return dao.findCustomerByName(name);
	}

	public void insertCustomer(Customer c) {
		dao.insertCustomer(c);
	}

	/**
	 * 批量保存, 编程式事务管理
	 */
	public void saveCustomers(final List<Customer> list) {
		for(Customer c : list){
			this.insertCustomer(c);
		}
	}

	public void updateCustomer(Customer c) {
		dao.updateCustomer(c);
	}
}

jdbc.properties

jdbc.driverclass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=root

c3p0.pool.size.max=10
c3p0.pool.size.min=2
c3p0.pool.size.ini=3
c3p0.pool.size.increment=2

hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=none

sh.xml,spring配置文件

<?xml version="1.0"?>
<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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-2.5.xsd 
                        http://www.springframework.org/schema/tx 
                        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
    <!-- 指定分散配置的文件的位置 -->
    <context:property-placeholder location="classpath:cn/itcast/spring/hibernate/tx25/jdbc.properties" />
    <!-- 配置c3p0数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverclass}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />

        <property name="maxPoolSize" value="${c3p0.pool.size.max}" />
        <property name="minPoolSize" value="${c3p0.pool.size.min}" />
        <property name="initialPoolSize" value="${c3p0.pool.size.ini}" />
        <property name="acquireIncrement" value="${c3p0.pool.size.increment}" />
    </bean>

    <!-- 本地回话工厂bean,spring整合hibernate的核心入口 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 指定hibernate自身的属性 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            </props>
        </property>
        <property name="mappingDirectoryLocations">
            <list>
                <value>classpath:cn/itcast/spring/domain</value>
            </list>
        </property>
    </bean>

    <!-- customerDao -->
    <bean id="customerDao" class="cn.itcast.spring.hibernate.tx25.CustomerDaoImpl">
        <property name="sf" ref="sessionFactory" />
    </bean>

    <!-- hibernate事务管理器,在service层面上实现事务管理,而且达到平台无关性 -->
    <bean id="htm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- customerService -->
    <bean id="customerService" class="cn.itcast.spring.hibernate.tx25.CustomerServiceImpl">
        <property name="dao" ref="customerDao" />
    </bean>
    
    <!-- 事务通知 -->
    <tx:advice id="txAdvice" transaction-manager="htm">
        <!-- 事务属性 -->
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT"/>
            <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>
            <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>
            <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/>
            <tx:method name="batch*" propagation="REQUIRED" isolation="DEFAULT"/>
            
            <tx:method name="load*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
            <tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
            <tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
            
            <tx:method name="*" propagation="SUPPORTS" isolation="DEFAULT" read-only="false"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- aop配置(配置切入点) -->
    <aop:config>
        <aop:pointcut id="txPointcut" expression="execution(* *..*Service.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
    </aop:config>
</beans>

App.java 测试代码

public class App {

	public static void main(String[] args) throws SQLException {
		ApplicationContext ac = new ClassPathXmlApplicationContext(
				"cn/itcast/spring/hibernate/tx25/sh.xml");
		CustomerService cs = (CustomerService) ac.getBean("customerService");
		List<Customer> list = new ArrayList<Customer>();
		Customer c = null ;
		for(int i = 0 ; i < 10 ; i++){
			c = new Customer();
			if(i == 9){
				c.setName(null);
			}
			else{
				c.setName("tom" + i);
			}
			c.setAge(20 + i);
			list.add(c);
		}
		cs.saveCustomers(list);
	}

}


2. aspectj 配置方法 代码示例

和(1)tx方式相比只有两个文件有变动

CustomerServiceImpl.java, service实现 以加注解的方式

/**
 * 通过注解方法实现事务管理
 */
@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT)
public class CustomerServiceImpl implements CustomerService {
	//dao
	private CustomerDao dao ;

	//注入dao
	public void setDao(CustomerDao dao) {
		this.dao = dao;
	}

	/**
	 * 只读
	 */
	@Transactional(readOnly=true)
	public List<Customer> findCustomerByName(String name) {
		return dao.findCustomerByName(name);
	}

	public void insertCustomer(Customer c) {
		dao.insertCustomer(c);
	}

	/**
	 * 批量保存, 编程式事务管理
	 */
	public void saveCustomers(final List<Customer> list) {
		for(Customer c : list){
			this.insertCustomer(c);
		}
	}

	public void updateCustomer(Customer c) {
		dao.updateCustomer(c);
	}
}

sh.xml 配置文件

<?xml version="1.0"?>
<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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-2.5.xsd 
                        http://www.springframework.org/schema/tx 
                        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
    <!-- 指定分散配置的文件的位置 -->
    <context:property-placeholder location="classpath:cn/itcast/spring/hibernate/tx25/jdbc.properties" />
    <!-- 配置c3p0数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverclass}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />

        <property name="maxPoolSize" value="${c3p0.pool.size.max}" />
        <property name="minPoolSize" value="${c3p0.pool.size.min}" />
        <property name="initialPoolSize" value="${c3p0.pool.size.ini}" />
        <property name="acquireIncrement" value="${c3p0.pool.size.increment}" />
    </bean>

    <!-- 本地回话工厂bean,spring整合hibernate的核心入口 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 指定hibernate自身的属性 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            </props>
        </property>
        <property name="mappingDirectoryLocations">
            <list>
                <value>classpath:cn/itcast/spring/domain</value>
            </list>
        </property>
    </bean>

    <!-- customerDao -->
    <bean id="customerDao" class="cn.itcast.spring.hibernate.aspectj.CustomerDaoImpl">
        <property name="sf" ref="sessionFactory" />
    </bean>

    <!-- hibernate事务管理器,在service层面上实现事务管理,而且达到平台无关性 -->
    <bean id="htm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- customerService -->
    <bean id="customerService" class="cn.itcast.spring.hibernate.aspectj.CustomerServiceImpl">
        <property name="dao" ref="customerDao" />
    </bean>
    
    <!-- 使用注解驱动 -->
    <tx:annotation-driven transaction-manager="htm"/>
</beans>







 

posted @ 2014-03-14 16:27  今晚打酱油_  阅读(295)  评论(0编辑  收藏  举报