最终通知

最终通知

* 在目标类的方法执行之后执行,如果程序出现了异常,最终通知也会执行。
* 配置文件信息:

<aop:after method="after" pointcut-ref="myPointcut3"/>

一、编写业务类

CustomerService:

package com.yiidian.service;
/**
 * @author http://www.yiidian.com
 *
 */
public interface CustomerService {

	public void save(String name);
	
	public void update();
}

CustomerServiceImpl:

package com.yiidian.service.impl;

import com.yiidian.service.CustomerService;
/**
 * 这个类在AOP属于目标对象(Target)
 * @author http://www.yiidian.com
 *
 */
public class CustomerServiceImpl implements CustomerService {

	@Override
	public void save(String name) {
		System.out.println("执行save方法,name为:"+name);
	}

	@Override
	public void update() {
		System.out.println("执行update方法");
	}

}

二、编写切面类,添加最终通知方法

package com.yiidian.aspect;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;

/**
 * Spring的AOP的切面类
 * @author http://www.yiidian.com
 *
 */
public class MyAspect {

	/**
	 * 最终通知
	 * JoinPoint:代表当前拦截的方法对象,使用该对象可以获取拦截方法的信息(例如:类名,方法名,方法参数等)
	 */
	public void after(JoinPoint jp){
		System.out.println("执行了后置通知");
		System.out.println("代理对象类型:"+jp.getThis().getClass());
		System.out.println("拦截的方法名称:"+jp.getSignature().getName());
		System.out.println("拦截方法的参数列表:"+Arrays.asList(jp.getArgs()));
	}

}

三、配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd">

	<!-- 基于AspectJ的spring的AOP编写之XML方式 -->
	
	<!-- 1.创建目标对象 -->
	<bean id="customerService" class="com.yiidian.service.impl.CustomerServiceImpl"/>
	
	<!-- 2.创建切面类对象 -->
	<bean id="myAspect" class="com.yiidian.aspect.MyAspect"/>
	
	<!-- 3.配置AOP切面 -->
	<aop:config>
		<!-- 切面 = 通知+切入点 -->
		<aop:aspect ref="myAspect">
			<!-- 
				aop:after:这个是最终通知配置
			 -->
			<aop:after method="after" pointcut-ref="pt"/>
			<aop:pointcut expression="execution(public void com.yiidian.service.impl.CustomerServiceImpl.*(..))" id="pt"/>
		</aop:aspect>
	</aop:config>
	
</beans>

 四、编写测试类

package com.yiidian.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.yiidian.service.CustomerService;
/**
 * @author http://www.yiidian.com
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1 {

	@Resource
	private CustomerService customerService;
	
	@Test
	public void test1(){
		customerService.save("一点教程网");
		customerService.update();
	}
}

五、运行结果

源码下载:http://pan.baidu.com/s/1dFfGHIL

热门文章

优秀文章