注解实在是太简单了,直接上代码得了
import org.springframework.stereotype.Component;
@Component
public class Play {
public Play() {
}
public void say() {//这里就是连接点
System.out.println("小鸭子,嘎嘎嘎嘎");
}
}
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class Log {
@Before("execution(* Play.say(..))")//定义切点
public void log() {//定义通知
System.out.println(" executed");
}
}
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class tests {
@Test//进行测试
public void test(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("config.xml");
((Play)applicationContext.getBean("play")).say();
}
}
结果正确
之后只需要学习有关通知位置的常用注解就好
常用的通知注解:前置通知@Before,后置通知@AfterReturning ,异常通知 @AfterThrowing,最终通知 @After,环绕通知@Around