AOP即面向切面编程。它的底层实际是用了spring的动态代理,具体是JDK的代理还是CGLIB的代理,就视情况而定了。本博客园仅仅作为平时记录,显得有些杂乱无章,如果想了解动态代理,设计模式,请访问我的个人博客。
1. 首先引入aop的jar包
1 <dependency> 2 <groupId>org.springframework</groupId> 3 <artifactId>spring-aspects</artifactId> 4 <version>5.0.8.RELEASE</version> 5 </dependency>
2. 被切的业务类
/** * description * * @author 70KG * @date 2018/7/30 */ public class BusinessCalculate { public int calculate(int i, int j) { int result = i / j; System.out.println("BusinessCalculate-业务方法执行。。。。。。"); return result; } }
3. 编写切面类
前置切面类:
/** * description * * @author 70KG * @date 2018/7/30 */ @Aspect public class BeforeAdvice { // 前置通知 @Before("com.nmys.story.springCore.springaop.test01.SystemArchitecture.pointCut()") public void logBefore(JoinPoint joinPoint) { // 获取传入的参数 Object[] args = joinPoint.getArgs(); for (int i = 0; i < args.length; i++) { System.out.println("傳入的參數:" + args[i]); } System.out.println("调用方法之前执行logBefore。。。。。。"); } }
后置切面类:
/** * description * * @author 70KG * @date 2018/7/30 */ @Aspect public class AfterAdvice { // 后置通知 @After("com.nmys.story.springCore.springaop.test01.SystemArchitecture.pointCut()") public void logAfter() { System.out.println("调用方法之后执行logAfter。。。。。。"); } }
切点类(为的提取公共切点):
/** * description 用来定义切入点的类 * * @author 70KG * @date 2018/7/30 */ public class SystemArchitecture { @Pointcut("bean(*domCalculate)") public void definitionPointCut(){} // execution(* *(..)) 所有方法 // 抽取公共表达式 // @Pointcut("execution(public int com.nmys.story.springCore.aopdemo.BusinessCalculate.*(..))") @Pointcut("execution(* com.nmys.story.springCore.springaop.test01.BusinessCalculate.*(..))") // @Pointcut("bean(*Calculate)") public void pointCut() {} }
4.编写配置类
最重要的是要开启aop注解@EnableAspectJAutoProxy
/** * @author 70KG * @Title: Config * @Description: 配置类 * @date 2018/7/29下午2:01 * @From www.nmyswls.com */ @Configuration // 手动开启Aspect注解 @EnableAspectJAutoProxy public class AopConfig { // 将@Aspect修饰的类和业务类都交给spring来管理 @Bean public BeforeAdvice beforeAdvice() { return new BeforeAdvice(); } @Bean public AfterAdvice afterAdvice() { return new AfterAdvice(); } @Bean public BusinessCalculate businessCalculate() { return new BusinessCalculate(); } }
5. 测试
/** * @author 70KG * @Title: MainTest * @Description: * @date 2018/7/29下午2:01 * @From www.nmyswls.com */ public class MainTest { public static void main(String[] args) { // 加载配置文件 AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AopConfig.class); BusinessCalculate businessCalculate = ac.getBean(BusinessCalculate.class); System.out.println("==================================================="); int result1 = businessCalculate.calculate(10, 5); System.out.println("計算結果:" + result1); System.out.println("==================================================="); // RandomCalculate randomCalculate = ac.getBean(RandomCalculate.class); // int result2 = randomCalculate.calculate(10, 5); // System.out.println("計算結果:" + result2); } }
6. 结果
=================================================== 傳入的參數:10 傳入的參數:5 调用方法之前执行logBefore。。。。。。 BusinessCalculate-业务方法执行。。。。。。 调用方法之后执行logAfter。。。。。。 計算結果:2 ===================================================