Spring2.5注解事务配置

service.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:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
		default-autowire="byName" default-lazy-init="true">
	<context:component-scan
		base-package="org.zlex.spring.service" />
	
	<context:property-placeholder location="classpath:jdbc.properties"/>	
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${jdbc.mysql.className}"></property>
		<property name="url" value="${jdbc.mysql.url}"></property>
		<property name="username" value="${jdbc.mysql.user}"></property>
		<property name="password" value="${jdbc.mysql.password}"></property>
	</bean>
	
	<!-- 设定transactionManager事务管理器 -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 启动spring注解功能,需要cglib-nodep-x.x_x.jar支持 -->
	<tx:annotation-driven transaction-manager="txManager" />
	
</beans>

 xml顶部需要加上:xmlns:tx=http://www.springframework.org/schema/tx

xsi:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" 支持事务。

AccountServiceImpl.java

package org.zlex.spring.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zlex.spring.dao.AccountDao;
import org.zlex.spring.domain.Account;
import org.zlex.spring.service.AccountService;

@Service
@Transactional
public class AccountServiceImpl implements AccountService {

	@Autowired
	private AccountDao accountDao;
	
	public boolean verify(String username, String password) {
		
		Account account = accountDao.read(username);
		if (password.equals(account.getPassword())) {
			return true;
		} else {
			return false;
		}
	}

}

 

/**
 * 2013-5-2
 */
package org.zlex.spring.test;

import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.zlex.spring.dao.AccountDao;
import org.zlex.spring.domain.Account;
import org.zlex.spring.service.AccountService;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:service.xml"})   
public class Test2 extends AbstractJUnit4SpringContextTests {
	@Autowired
	AccountService accountService;
	
	
	@Before
	public void setUp() throws Exception{
		System.out.println("setUp ...");
	}
    @After
    public void tearDown() throws Exception{
    	System.out.println("tearDown ...");
    }
    
	@org.junit.Test
    public void findAll()
    {
		
		System.out.println(accountService.verify("wolf", "wolf"));
		System.out.println(accountService.verify("123", "123"));
   }

}

 jdbc.properties

#mysql configuration
jdbc.mysql.className = com.mysql.jdbc.Driver
jdbc.mysql.url = jdbc:mysql://localhost:3306/sms
jdbc.mysql.user = test
jdbc.mysql.password = test
jdbc.mysql.dialect = org.hibernate.dialect.MySQL5Dialect

 

支持jar包~!

cglib-nodep-2.1_3.jar

commons-dbcp.jar

commons-pool.jar

mysql-connector-java-3.1.11.jar 并启动mysql服务~!

Spring2.5注解事务说明~!

  1. 如果事务管理器的id是transactionManger,这里可以不对transaction-manager进行配置,即<tx:annotation-driven />就可以了。
  2. 这个配置是告诉spring在类(接口)层面或者方法层面上检查应用程序上下文中的所有标准了@Transactional的bean,spring将自动把事务通知的内容通知给它。
  3. 这个通知的事务参数将由@Transactional注释的参数来定义
  4. 如果注释的是接口,则该接口的所有实现类都将被事务化。

 

posted @ 2013-05-03 18:04  全新时代-小小程序员大梦想  阅读(371)  评论(0编辑  收藏  举报