基于Spring的DataSourceTransactionMananger类事务管理器配置声明式事务
<?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.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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 使用spring注解 --> <!-- <context:annotation-config/> --> <!-- 下面的配置告诉spring容器自动scanner用注解标注bean --> <context:component-scan base-package="org.lian"/> <!--配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp2.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="/conf/datasource/jdbc.properties"/> <!-- spring 声明式事务配置 --> <!--transaction manager 声明事务管理器 使用spring PlatformTransactionManager--> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置事务策略 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <!-- the transactional semantics... --> <tx:attributes> <!-- all methods starting with 'get' are read-only --> <tx:method name="get*" read-only="true"/> <tx:method name="count*" read-only="true"/> <!-- other methods use the default transaction settings (see below) --> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- execution(* org.lian.service.ContactsManagerService.*(..)) --> <!-- execution(* org.lian.service.*.*(..)) 包下的所有方法 --> <!-- execution(* org.lian.service..*Service.*(..)) 包下Service结尾Interface--> <aop:config> <aop:pointcut id="serviceOperation" expression="execution(* org.lian.service..*Service.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/> </aop:config> </beans>
service层java interface
package org.lian.service;
import org.lian.domain.Contacts;
public interface ContactsManagerService {
public Contacts getContacts(Contacts contacts);
public void addContacts(Contacts contacts);
public void editContacts(Contacts contacts);
}
实现类
@Service("contactsManagerService")
public class ContactsManagerServiceImpl implements ContactsManagerService {
@Autowired
@Qualifier("contactsDao")
private ContactsDao contactsDao;
public ContactsDao getContactsDao() {
return contactsDao;
}
public void setContactsDao(ContactsDao contactsDao) {
this.contactsDao = contactsDao;
}
@Autowired
@Qualifier("musicDao")
private MusicDao musicDao;
public MusicDao getMusicDao() {
return musicDao;
}
public void setMusicDao(MusicDao musicDao) {
this.musicDao = musicDao;
}
@Override
public Contacts getContacts(Contacts contacts) {
// TODO Auto-generated method stub
return null;
}
@Override
public void addContacts(Contacts contacts) {
Music music = new Music();
music.setId(IdGenerator.getInstance().getUUID());
music.setName("唇语");
music.setSinger("任然");
music.setAlbum("从小到大");
music.setAuthor("耕耕");
music.setComposer("Xun");
music.setStyle("流行");
music.setDuration("04:48");
// 添加一个music对象
musicDao.add(music);
contactsDao.insertContacts(contacts);
// 事务
}
@Override
public void editContacts(Contacts contacts) {
// TODO Auto-generated method stub
}
}