后置增强

/ Spring / 没有评论 / 407浏览

在上一篇中我们了解了前置增强的使用,在这一篇中我们看一下后置增强的具体使用。由于之前的文章中已经做了详细的说明,这里就不在做过多的介绍了,我们直接看下面的事例。

public class UserServiceAfterAdvice implements AfterReturningAdvice{

@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1)
throws Throwable {
System.out.println("spring增强");
TransactionManager.commit();
}
}
<bean id="userServiceBeforeAdvice" class="com.jilinwula.spring.aop.UserServiceBeforeAdvice"/>
<bean id="userServiceAfterAdvice" class="com.jilinwula.spring.aop.UserServiceAfterAdvice"/>
<bean id="target" class="com.jilinwula.spring.aop.UserServiceImpl"/>
<bean id="userService" class="org.springframework.aop.framework.ProxyFactoryBean"
p:proxyInterfaces="com.jilinwula.spring.aop.IUserService"
p:interceptorNames="userServiceBeforeAdvice,userServiceAfterAdvice"
p:target-ref="target"/>
@Test
public void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
IUserService userService = applicationContext.getBean("userService", IUserService.class);
userService.register("admin","吉林乌拉","jilinwula");
}
spring增强
开启事物
用户注册: username: admin nickName: 吉林乌拉 password: jilinwula
spring增强
提交事物