AI智能
改变未来

学习笔记(05-03):轻松搞定Spring全家桶(初识篇)-面向切片编程AOP:使用AspectJ基于Annotation开发AOP

与前边学习笔记(04-05):轻松搞定Spring全家桶(初识篇)-控制反转IOC:基于Annotation装配Bean中的应用类似。
使用AspectJ基于Annotation开发AOP:
一、创建项目,导入相关jar包:(点击下载)

  1. spring项目所需基础jar包
  2. 使用AspectJ开发AOP额外需要的jar包

二、在配置文件中加入aop的命名空间

三、具体代码:

  1. 配置文件:/AOPProjectByAspectJAnnotation/resource/applicationContext.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\"xmlns:context=\"http://www.springframework.org/schema/context\"xsi:schemaLocation=\"http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd\"><!-- 配置需要自动扫描的包,使得注解起作用 --><context:component-scan base-package=\"com.aop\"></context:component-scan><!-- 开启切面的自动代理:使得切面可以使用让aspectj注解起作用: 自动为匹配的类生成代理对象 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>
  1. 接口类:/AOPProjectByAspectJAnnotation/src/com/aop/UserDao.java
package com.aop;public interface UserDao {public void addUser(String id,String name);}
  1. 接口实现类:/AOPProjectByAspectJAnnotation/src/com/aop/UserDaoImpl.java
package com.aop;import org.springframework.stereotype.Repository;@Repository(\"userDao\")public class UserDaoImpl implements UserDao {@Overridepublic void addUser(String id, String name) {// TODO Auto-generated method stub//人为制造异常//String a=null;a.charAt(0);System.out.println(\"执行逻辑:\"+name+\" 的id是:\"+id);System.out.println(\"-----------方法逻辑结束-----------\");}}
  1. 切面类:/AOPProjectByAspectJAnnotation/src/com/aop/LogAspect.java
package com.aop;import org.aspectj.lang.JoinPoint;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;import org.springframework.stereotype.Component;//声明他是切面类@Aspect//声明他是一个bean(组件)@Componentpublic class LogAspect {/*** 定义一个方法,用于声明切入点表达式,一般该方法中不需要再添加代码。* 使用@Pointcut来声明切入点表达式* 后面的其他通知直接使用方法名来引用当前的切入点表达式*/@Pointcut(\"execution(* com.aop.UserDao.addUser(String,String))\")public void myPointCut() {}//before//这里要填写切入点,填写内容就是声明为切入点的方法名@Before(\"myPointCut()\")public void myBefore(JoinPoint jp) {System.out.println(\"===before-目标对象:\"+jp.getTarget());System.out.println(\"------------before end-------------------\");}//after@After(\"myPointCut()\")public void myAfter(JoinPoint jp) {System.out.println(\"===after-目标对象:\"+jp.getTarget());System.out.println(\"--------目标异常也会执行---------------\");}//around@Around(\"myPointCut()\")public Object myAround(ProceedingJoinPoint pjp) throws Throwable {System.out.println(\"=======around start::\"+pjp.getTarget());System.out.println(\"---------around head end------------\");Object object = pjp.proceed();System.out.println(\"=======around end::\"+pjp.getTarget());System.out.println(\"---------around foot end------------\");return object;}//after-returning@AfterReturning(\"myPointCut()\")public void myAfterReturning(JoinPoint jp) {System.out.println(\"===after-returning start=====\");System.out.println(\"--------目标异常不执行---------------\");}//throwing@AfterThrowing(value=\"myPointCut()\",throwing=\"e\")public void myAfterThrowing(JoinPoint jp,Throwable e) {System.out.println(\"error:\"+e);}}
  1. 测试类:/AOPProjectByAspectJAnnotation/src/com/aop/TestUserDao.java
package com.aop;import static org.junit.jupiter.api.Assertions.*;import org.junit.jupiter.api.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;class TestUserDao {@Testvoid testAddUser() {ApplicationContext context = new ClassPathXmlApplicationContext(\"applicationContext.xml\");UserDao userDao = context.getBean(\"userDao\",UserDao.class);userDao.addUser(\"T101\", \"Tom\");}}

四、测试结果:

  1. 目标对象异常
  2. 目标对象正常
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » 学习笔记(05-03):轻松搞定Spring全家桶(初识篇)-面向切片编程AOP:使用AspectJ基于Annotation开发AOP