学习笔记:4_Spring_AOP之基于AspectJ注解配置AOP

1、  需要的jar包

学习笔记:4_Spring_AOP之基于AspectJ注解配置AOP

 

 

2、  接口

package com.spring.aop;


public interface IZoo {
public String dog(String s);
public String cat(String s);
public String pig(String s);
}

 

 

3、  接口实现类

package com.spring.aop;


import org.springframework.stereotype.Component;


@Component
public class AopZoo implements IZoo {
public String dog(String s) {
System.out.println("dog核心代码"+s);
return s;
}
public String cat(String s) {
System.out.println("cat核心代码"+s);
return s;
}
public String pig(String s) {
System.out.println("pig核心代码"+s);
return s;
}
}

 

 

 

4、  Aspect切面类

package com.spring.aop;


import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;


//通过@Aspect表示这是一个切面类
@Aspect
@Component
public class AopZooAspect {
//通过@Before表示这是一个前置通知
//在execution里面写切面表达式,表达式里是被代理方法的完整路径等信息
@Before("execution(public String com.spring.aop.AopZoo.pig(String))")
public void beforeAop(){
System.out.println("之前");
}

//@After表示这是一个后置通知,用法和@After一样
@After("execution(public String com.spring.aop.AopZoo.pig(String))")
public void afterAop(){
System.out.println("之后");
}
}

 

 

5、  配置文件

学习笔记:4_Spring_AOP之基于AspectJ注解配置AOP学习笔记:4_Spring_AOP之基于AspectJ注解配置AOP

 

6、  Main方法

学习笔记:4_Spring_AOP之基于AspectJ注解配置AOP

 

 

7、  执行结果

学习笔记:4_Spring_AOP之基于AspectJ注解配置AOP

 

 

8、 Aspect切面类中切点表达式的星(*)和点点(..)的用法,以及JoinPoint连接点接口的用法

学习笔记:4_Spring_AOP之基于AspectJ注解配置AOP学习笔记:4_Spring_AOP之基于AspectJ注解配置AOP