aop 注解 开启spring自带的事务
一些基本知识 可以
http://jinnianshilongnian.iteye.com/blog/1415606
serviceImpl.java
1 package cn.us.service.impl; 2 3 import java.util.List; 4 import java.util.UUID; 5 6 import javax.annotation.Resource; 7 8 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.beans.factory.annotation.Qualifier; 10 import org.springframework.stereotype.Service; 11 import org.springframework.transaction.annotation.Propagation; 12 import org.springframework.transaction.annotation.Transactional; 13 14 import cn.us.dao.IUserDao; 15 import cn.us.dao.impl.UserDaoImpl; 16 import cn.us.domain.User; 17 import cn.us.service.IUserService; 18 @Service(value="userService") 19 //@Transactional(propagation=Propagation.SUPPORTS,readOnly=true) 20 21 public class UserServiceImpl implements IUserService { 22 23 //@Resource(name="userDao") 24 @Autowired 25 @Qualifier(value="userDao") 26 private IUserDao userDao; 27 28 public User getOne(String uid) { 29 return userDao.getOne(uid); 30 } 31 //这个方法不需要通知 32 // @Transactional(propagation = Propagation.NOT_SUPPORTED) 33 public List<User> getAllUsers() { 34 return userDao.getAllUsers(); 35 } 36 37 public boolean isExistenceUser(User user) { 38 return userDao.isExistenceUser(user); 39 } 40 41 @Transactional(readOnly = false, propagation=Propagation.REQUIRED) 42 public User addUser(User user) { 43 user.setUid(UUID.randomUUID().toString()); 44 userDao.addUser(user); 45 return user; 46 } 47 }
xml配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.0.xsd 11 http://www.springframework.org/schema/tx 12 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 13 http://www.springframework.org/schema/aop 14 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 15 <!-- 注册注解配置 --> 16 <context:annotation-config/> 17 <!--注入 事务管理器 这个注解驱动 告诉 spring 注解驱动 具体上网搜索 --> 18 <tx:annotation-driven transaction-manager="transactionManager"/> 19 20 21 <!-- 扫描注解bean --> 22 <context:component-scan base-package="cn.us.aspect"/> 23 24 <!-- 开启切面代理 使得spring认识 @Aspect --> 25 <aop:aspectj-autoproxy/> 26 27 <!-- 事务管理器 --> 28 <bean id="transactionManager" 29 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 30 <property name="dataSource" ref="dataSource" /> 31 </bean> 32 33 34 <!-- 数据源 --> 35 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 36 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 37 <property name="url" value="jdbc:mysql://localhost/usersystem?characterEncoding=utf-8"></property> 38 <property name="username" value="root"></property> 39 <property name="password" value="123456"></property> 40 </bean> 41 42 43 <bean id="sqlsessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" > 44 <property name="configLocation" value="classpath:sqlMapConfig.xml"></property> 45 <property name="dataSource" ref="dataSource"></property> 46 </bean> 47 48 </beans>