Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
[ch06] Transaction code with ProxyFactoryBean.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed Sep 9, 2017
1 parent 4433fb7 commit 704e309
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
32 changes: 32 additions & 0 deletions src/main/java/ch01/springbook/user/TransactionAdvice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ch01.springbook.user;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

public class TransactionAdvice implements MethodInterceptor {

private PlatformTransactionManager transactionManager;

public void setTransactionManager(PlatformTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}

public Object invoke(MethodInvocation invocation) throws Throwable {

TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());

try {

Object ret = invocation.proceed();
transactionManager.commit(status);
return ret;

} catch (RuntimeException e) {
transactionManager.rollback(status);
throw e;
}
}
}
24 changes: 23 additions & 1 deletion src/main/resources/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,35 @@
<!-- <bean id="userService" class="ch01.springbook.user.UserServiceTx">
<property name="transactionManager" ref="transactionManager" />
<property name="userService" ref="userServiceImpl" />
</bean> -->
</bean>
<bean id="userService" class="ch01.springbook.user.TxProxyFactoryBean">
<property name="target" ref="userServiceImpl" />
<property name="transactionManager" ref="transactionManager" />
<property name="pattern" value="upgradeLevels" />
<property name="serviceInterface" value="ch01.springbook.user.UserService" />
</bean> -->

<bean id="transactionAdvice" class="ch01.springbook.user.TransactionAdvice">
<property name="transactionManager" ref="transactionManager"/>
</bean>

<bean id="transactionPointcut" class="org.springframework.aop.support.NameMatchMethodPointcut">
<property name="mappedName" value="upgrade*"/>
</bean>

<bean id="transactionAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice" ref="transactionAdvice"/>
<property name="pointcut" ref="transactionPointcut"/>
</bean>

<bean id="userService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="userServiceImpl"/>
<property name="interceptorNames">
<list>
<value>transactionAdvisor</value>
</list>
</property>
</bean>

<bean id="message" class="ch06.springbook.factorybean.MessageFactoryBean">
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/ch01/springbook/user/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.annotation.DirtiesContext;
Expand Down Expand Up @@ -177,7 +178,7 @@ public void upgradeAllOrNothingUsingDynamicProxyBean() throws Exception {
TestUserService testUserService = new TestUserService(userList.get(3).getId());
testUserService.setUserDao(userDao);

TxProxyFactoryBean txProxyFactoryBean = applicationContext.getBean("&userService", TxProxyFactoryBean.class);
ProxyFactoryBean txProxyFactoryBean = applicationContext.getBean("&userService", ProxyFactoryBean.class);
txProxyFactoryBean.setTarget(testUserService);

UserService txUserService = (UserService)txProxyFactoryBean.getObject();
Expand Down

0 comments on commit 704e309

Please sign in to comment.