编写好的xml文件
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy/> <bean id="myIntercept" class="intercept.MyIntercept"></bean> <bean id="person" class="impl.PersonImpl"></bean> </beans>
所需的接口
package servlice;
public interface Person {
public void eat(String name);
public String sleep();
}
实现类
package impl;
import servlice.Person;
public class PersonImpl implements Person{
public void eat(String name) {
System.out.println("吃饭了");
}
public String sleep() {
System.out.println("睡觉去");
return "睡的好香啊";
}
}
拦截器对函数参数的调用
package intercept;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class MyIntercept {
@Pointcut("execution(* impl.PersonImpl.eat(..))")//修改了这里
private void eat(){}
// 第一个*代表所有的返回类型
// eat表示类里面的eat方法
@Before("eat()&& args(name)")
private void beforeEat(String name)
{
System.out.println(name+"吃饭前先洗手");
}
}
测试类
package text;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import servlice.Person;
public class AopText {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Person person = (Person) context.getBean("person");
person.eat("小朋友+");
}
}
测试结果
小朋友+吃饭前先洗手
吃饭了
说明参数已经正确传递到拦截器中了。
调用返回值,修改拦截器
package intercept;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class MyIntercept {
@Pointcut("execution(* impl.PersonImpl.sleep(..))")//修改了这里
private void sleep(){}
// 第一个*代表所有的返回类型
// eat表示类里面的eat方法
@AfterReturning(pointcut="sleep()",returning="result")
private void afterSleep(String result)
{
System.out.println(result);
System.out.println("睡觉起来也要刷牙洗脸");
}
}
修改测试函数
package text;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import servlice.Person;
public class AopText {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Person person = (Person) context.getBean("person");
person.sleep();
}
}
测试结果:
睡觉去
睡的好香啊
睡觉起来也要刷牙洗脸
调用例外信息,修改拦截器
package intercept;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class MyIntercept {
@Pointcut("execution(* impl.PersonImpl.sleep(..))")//修改了这里
private void sleep(){}
// 第一个*代表所有的返回类型
@AfterThrowing(pointcut="sleep()",throwing="e")
private void SleepException(Exception e)
{
System.out.println(e);
System.out.println("睡觉起来也要刷牙洗脸");
}
}
修改实现类
package impl;
import servlice.Person;
public class PersonImpl implements Person{
public void eat(String name) {
System.out.println("吃饭了");
}
public String sleep() {
throw new RuntimeException("出错了");
}
}
测试结果
java.lang.RuntimeException: 出错了
睡觉起来也要刷牙洗脸