(1)配置:
Spring的事務管理是通過AOP代理實現的,其中的事務通知由元數據驅動。代理對象與事務元數據結合產生一個AOP代理,它使用一個PlatformTransactionManager實現,配合TransactionInterceptor,在方法調用前后實施事務。
<?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-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">
<description>springApp</description>
<!-- dataSource for MySQL -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/springapp" />
<property name="username" value="root" />
<property name="password" value="****" />
</bean>
<!-- Hibernate SessionFactory for MySQL -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">100</prop>
</props>
</property>
</bean>
<!--Transaction -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<aop:config> <!--use CGLIB:proxy-target-class="true-->
<aop:pointcut id="serviceOperator" expression="execution(* com.logcd.business.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperator"/>
<!--
<aop:advisor pointcut="execution(* com.logcd.business.service..*Service.*(..))" advice-ref="txAdvice"/>
-->
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" read-only="true" />
<tx:method name="load*" read-only="true" />
<tx:method name="is*" read-only="true"/>
<tx:method name="save*"
rollback-for="Exception"/>
<tx:method name="insert*"
rollback-for="Exception" />
<tx:method name="remove*"
rollback-for="Exception"/>
<tx:method name="add*"
no-rollback-for="Exception" />
</tx:attributes>
</tx:advice>
<!--Transaction -->
<!-- DAO -->
<bean id="genericDao" lazy-init="true" abstract="true"
class="com.logcd.bo.dao.impl.GenericDaoImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="customersDao" parent="genericDao"
class="com.logcd.bo.dao.impl.CustomersDaoImpl" />
<bean id="customerDao" parent="genericDao"
class="com.logcd.bo.dao.impl.CustomerDaoImpl" />
<bean id="addressDao" parent="genericDao"
class="com.logcd.bo.dao.impl.AddressDaoImpl" />
<bean id="customerManageService"
class="com.logcd.business.service.impl.CustomerManageServiceImpl"
autowire="byName"/>
</beans>
(2)測試
- package com.logcd.test;
-
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import com.logcd.bo.Customers;
- import com.logcd.business.service.CustomerManageService;
-
- import junit.framework.TestCase;
-
- public class SpringServiceTest extends TestCase {
-
- private CustomerManageService customerManageService;
-
- protected void setUp() throws Exception {
- super.setUp();
- ApplicationContext app = new ClassPathXmlApplicationContext("appContext.xml");
- customerManageService = (CustomerManageService) app.getBean("customerManageService");
- }
-
- protected void tearDown() throws Exception {
- super.tearDown();
- }
-
- public void testService() throws Exception{
- Customers cus = new Customers();
- cus.setName("testService");
- cus.setAge(29);
- customerManageService.saveCustomers(cus);
- }
- }
package com.logcd.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.logcd.bo.Customers;
import com.logcd.business.service.CustomerManageService;
import junit.framework.TestCase;
public class SpringServiceTest extends TestCase {
private CustomerManageService customerManageService;
protected void setUp() throws Exception {
super.setUp();
ApplicationContext app = new ClassPathXmlApplicationContext("appContext.xml");
customerManageService = (CustomerManageService) app.getBean("customerManageService");
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testService() throws Exception{
Customers cus = new Customers();
cus.setName("testService");
cus.setAge(29);
customerManageService.saveCustomers(cus);
}
}
附:pointcut里的語法
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)其中帶問號的modifiers-pattern?(public/protected) 和 declaring-type-pattern? throws-pattern? 可以不填
如execution(* *..BookManager.save(..))
第一顆* 代表ret-type-pattern 返回值可任意,
*..BookManager 代表任意Pacakge里的BookManager類。
如果寫成com.xyz.service.* 則代表com.xyz.service下的任意類
com.xyz.service..* com.xyz.service則代表com.xyz.service及其子package下的任意類
save代表save方法,也可以寫save* 代表saveBook()等方法
(..) 匹配0個參數或者多個參數的,任意類型
(x,..) 第一個參數的類型必須是X
(x,,,s,..) 匹配至少4個參數,第一個參數必須是x類型,第二個和第三個參數可以任意,第四個必須是s類型。