AOP:(Aspect Oriented Programming)面向切面编程,一种在已存在代码段中动态嵌入其他代码段的技术,用来在不改变原有代码的情况下添加一些新的通用功能,如记录日志、参数预处理等。
**通知Advice:**需要在原有程序中新增的功能;
**连接点JoinPoint:**程序中可以接入通知的点,包括类、方法等;
**切入点PointCut:**程序运行过程中实际接入通知的点,是一种特殊的连接点;
**切面Aspect:**是一个类,在这个类中定义切入点和通知;
原程序如下
@Service public class AopTestService { @CutPointAnno public void test(){ System.out.println("测试方法执行"); } @CutPointAnno public void testThrowable(){ throw new RuntimeException(); } } //此注解标注的方法被标记为切面 public @interface CutPointAnno { }切面,包括以注解形式定义的切入点和通知,这里定义了SpringAOP提供的四种通知类型(前置通知,后置通知,环绕通知,异常通知)其中,异常通知在程序抛出异常时执行
@Aspect @Component public class SimpleAspect { @Pointcut("@annotation(cn.sunyog.aop.CutPointAnno)") public void pointCut(){} @Before("pointCut()") public void doBefore(JoinPoint joinPoint) { System.out.println("前置通知"); } @After("pointCut()") public void doAfter(JoinPoint joinPoint){ System.out.println("后置通知"); } @Around("pointCut()") public void doAround(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("环绕通知,前段"); joinPoint.proceed();//程序执行 System.out.println("环绕通知,后段"); } @AfterThrowing("pointCut()") public void doThrowing(JoinPoint joinPoint){ System.out.println("异常通知"); } }启动Aop代理配置
@Configuration @ComponentScan("cn.sunyog.aop")//包扫描,扫描此包中所有的Bean,注册到容器中 @EnableAspectJAutoProxy//启用SpringAOP代理,包括jdk动态代理和cglib代理两种形式 public class AopConfig { }启动容器,测试Aop是否成功
public class AOPTest { private ApplicationContext ctx; @Before public void doBefore(){ ctx=new AnnotationConfigApplicationContext(AopConfig.class); } @After public void doAfter(){ } @Test public void test(){ AopTestService service = ctx.getBean(AopTestService.class); service.test(); System.out.println(); service.testThrowable(); } }打印结果
环绕通知,前段 前置通知 测试方法执行 后置通知 环绕通知,后段 环绕通知,前段 前置通知 异常通知 后置通知可见,通知的执行顺序为:
#mermaid-svg-ul62DcX7s2CQYwrM .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-ul62DcX7s2CQYwrM .label text{fill:#333}#mermaid-svg-ul62DcX7s2CQYwrM .node rect,#mermaid-svg-ul62DcX7s2CQYwrM .node circle,#mermaid-svg-ul62DcX7s2CQYwrM .node ellipse,#mermaid-svg-ul62DcX7s2CQYwrM .node polygon,#mermaid-svg-ul62DcX7s2CQYwrM .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-ul62DcX7s2CQYwrM .node .label{text-align:center;fill:#333}#mermaid-svg-ul62DcX7s2CQYwrM .node.clickable{cursor:pointer}#mermaid-svg-ul62DcX7s2CQYwrM .arrowheadPath{fill:#333}#mermaid-svg-ul62DcX7s2CQYwrM .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-ul62DcX7s2CQYwrM .flowchart-link{stroke:#333;fill:none}#mermaid-svg-ul62DcX7s2CQYwrM .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-ul62DcX7s2CQYwrM .edgeLabel rect{opacity:0.9}#mermaid-svg-ul62DcX7s2CQYwrM .edgeLabel span{color:#333}#mermaid-svg-ul62DcX7s2CQYwrM .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-ul62DcX7s2CQYwrM .cluster text{fill:#333}#mermaid-svg-ul62DcX7s2CQYwrM div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-ul62DcX7s2CQYwrM .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-ul62DcX7s2CQYwrM text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-ul62DcX7s2CQYwrM .actor-line{stroke:grey}#mermaid-svg-ul62DcX7s2CQYwrM .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-ul62DcX7s2CQYwrM .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-ul62DcX7s2CQYwrM #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-ul62DcX7s2CQYwrM .sequenceNumber{fill:#fff}#mermaid-svg-ul62DcX7s2CQYwrM #sequencenumber{fill:#333}#mermaid-svg-ul62DcX7s2CQYwrM #crosshead path{fill:#333;stroke:#333}#mermaid-svg-ul62DcX7s2CQYwrM .messageText{fill:#333;stroke:#333}#mermaid-svg-ul62DcX7s2CQYwrM .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-ul62DcX7s2CQYwrM .labelText,#mermaid-svg-ul62DcX7s2CQYwrM .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-ul62DcX7s2CQYwrM .loopText,#mermaid-svg-ul62DcX7s2CQYwrM .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-ul62DcX7s2CQYwrM .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-ul62DcX7s2CQYwrM .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-ul62DcX7s2CQYwrM .noteText,#mermaid-svg-ul62DcX7s2CQYwrM .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-ul62DcX7s2CQYwrM .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-ul62DcX7s2CQYwrM .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-ul62DcX7s2CQYwrM .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-ul62DcX7s2CQYwrM .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ul62DcX7s2CQYwrM .section{stroke:none;opacity:0.2}#mermaid-svg-ul62DcX7s2CQYwrM .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-ul62DcX7s2CQYwrM .section2{fill:#fff400}#mermaid-svg-ul62DcX7s2CQYwrM .section1,#mermaid-svg-ul62DcX7s2CQYwrM .section3{fill:#fff;opacity:0.2}#mermaid-svg-ul62DcX7s2CQYwrM .sectionTitle0{fill:#333}#mermaid-svg-ul62DcX7s2CQYwrM .sectionTitle1{fill:#333}#mermaid-svg-ul62DcX7s2CQYwrM .sectionTitle2{fill:#333}#mermaid-svg-ul62DcX7s2CQYwrM .sectionTitle3{fill:#333}#mermaid-svg-ul62DcX7s2CQYwrM .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ul62DcX7s2CQYwrM .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-ul62DcX7s2CQYwrM .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ul62DcX7s2CQYwrM .grid path{stroke-width:0}#mermaid-svg-ul62DcX7s2CQYwrM .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-ul62DcX7s2CQYwrM .task{stroke-width:2}#mermaid-svg-ul62DcX7s2CQYwrM .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ul62DcX7s2CQYwrM .taskText:not([font-size]){font-size:11px}#mermaid-svg-ul62DcX7s2CQYwrM .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ul62DcX7s2CQYwrM .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-ul62DcX7s2CQYwrM .task.clickable{cursor:pointer}#mermaid-svg-ul62DcX7s2CQYwrM .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-ul62DcX7s2CQYwrM .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-ul62DcX7s2CQYwrM .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-ul62DcX7s2CQYwrM .taskText0,#mermaid-svg-ul62DcX7s2CQYwrM .taskText1,#mermaid-svg-ul62DcX7s2CQYwrM .taskText2,#mermaid-svg-ul62DcX7s2CQYwrM .taskText3{fill:#fff}#mermaid-svg-ul62DcX7s2CQYwrM .task0,#mermaid-svg-ul62DcX7s2CQYwrM .task1,#mermaid-svg-ul62DcX7s2CQYwrM .task2,#mermaid-svg-ul62DcX7s2CQYwrM .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-ul62DcX7s2CQYwrM .taskTextOutside0,#mermaid-svg-ul62DcX7s2CQYwrM .taskTextOutside2{fill:#000}#mermaid-svg-ul62DcX7s2CQYwrM .taskTextOutside1,#mermaid-svg-ul62DcX7s2CQYwrM .taskTextOutside3{fill:#000}#mermaid-svg-ul62DcX7s2CQYwrM .active0,#mermaid-svg-ul62DcX7s2CQYwrM .active1,#mermaid-svg-ul62DcX7s2CQYwrM .active2,#mermaid-svg-ul62DcX7s2CQYwrM .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-ul62DcX7s2CQYwrM .activeText0,#mermaid-svg-ul62DcX7s2CQYwrM .activeText1,#mermaid-svg-ul62DcX7s2CQYwrM .activeText2,#mermaid-svg-ul62DcX7s2CQYwrM .activeText3{fill:#000 !important}#mermaid-svg-ul62DcX7s2CQYwrM .done0,#mermaid-svg-ul62DcX7s2CQYwrM .done1,#mermaid-svg-ul62DcX7s2CQYwrM .done2,#mermaid-svg-ul62DcX7s2CQYwrM .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-ul62DcX7s2CQYwrM .doneText0,#mermaid-svg-ul62DcX7s2CQYwrM .doneText1,#mermaid-svg-ul62DcX7s2CQYwrM .doneText2,#mermaid-svg-ul62DcX7s2CQYwrM .doneText3{fill:#000 !important}#mermaid-svg-ul62DcX7s2CQYwrM .crit0,#mermaid-svg-ul62DcX7s2CQYwrM .crit1,#mermaid-svg-ul62DcX7s2CQYwrM .crit2,#mermaid-svg-ul62DcX7s2CQYwrM .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-ul62DcX7s2CQYwrM .activeCrit0,#mermaid-svg-ul62DcX7s2CQYwrM .activeCrit1,#mermaid-svg-ul62DcX7s2CQYwrM .activeCrit2,#mermaid-svg-ul62DcX7s2CQYwrM .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-ul62DcX7s2CQYwrM .doneCrit0,#mermaid-svg-ul62DcX7s2CQYwrM .doneCrit1,#mermaid-svg-ul62DcX7s2CQYwrM .doneCrit2,#mermaid-svg-ul62DcX7s2CQYwrM .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-ul62DcX7s2CQYwrM .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-ul62DcX7s2CQYwrM .milestoneText{font-style:italic}#mermaid-svg-ul62DcX7s2CQYwrM .doneCritText0,#mermaid-svg-ul62DcX7s2CQYwrM .doneCritText1,#mermaid-svg-ul62DcX7s2CQYwrM .doneCritText2,#mermaid-svg-ul62DcX7s2CQYwrM .doneCritText3{fill:#000 !important}#mermaid-svg-ul62DcX7s2CQYwrM .activeCritText0,#mermaid-svg-ul62DcX7s2CQYwrM .activeCritText1,#mermaid-svg-ul62DcX7s2CQYwrM .activeCritText2,#mermaid-svg-ul62DcX7s2CQYwrM .activeCritText3{fill:#000 !important}#mermaid-svg-ul62DcX7s2CQYwrM .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ul62DcX7s2CQYwrM g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-ul62DcX7s2CQYwrM g.classGroup text .title{font-weight:bolder}#mermaid-svg-ul62DcX7s2CQYwrM g.clickable{cursor:pointer}#mermaid-svg-ul62DcX7s2CQYwrM g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-ul62DcX7s2CQYwrM g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-ul62DcX7s2CQYwrM .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-ul62DcX7s2CQYwrM .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-ul62DcX7s2CQYwrM .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-ul62DcX7s2CQYwrM .dashed-line{stroke-dasharray:3}#mermaid-svg-ul62DcX7s2CQYwrM #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ul62DcX7s2CQYwrM #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ul62DcX7s2CQYwrM #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-ul62DcX7s2CQYwrM #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-ul62DcX7s2CQYwrM #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ul62DcX7s2CQYwrM #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ul62DcX7s2CQYwrM #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ul62DcX7s2CQYwrM #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ul62DcX7s2CQYwrM .commit-id,#mermaid-svg-ul62DcX7s2CQYwrM .commit-msg,#mermaid-svg-ul62DcX7s2CQYwrM .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ul62DcX7s2CQYwrM .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ul62DcX7s2CQYwrM .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ul62DcX7s2CQYwrM g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ul62DcX7s2CQYwrM g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-ul62DcX7s2CQYwrM g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-ul62DcX7s2CQYwrM g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-ul62DcX7s2CQYwrM g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-ul62DcX7s2CQYwrM g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-ul62DcX7s2CQYwrM .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-ul62DcX7s2CQYwrM .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-ul62DcX7s2CQYwrM .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-ul62DcX7s2CQYwrM .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-ul62DcX7s2CQYwrM .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-ul62DcX7s2CQYwrM .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-ul62DcX7s2CQYwrM .edgeLabel text{fill:#333}#mermaid-svg-ul62DcX7s2CQYwrM .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ul62DcX7s2CQYwrM .node circle.state-start{fill:black;stroke:black}#mermaid-svg-ul62DcX7s2CQYwrM .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-ul62DcX7s2CQYwrM #statediagram-barbEnd{fill:#9370db}#mermaid-svg-ul62DcX7s2CQYwrM .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-ul62DcX7s2CQYwrM .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-ul62DcX7s2CQYwrM .statediagram-state .divider{stroke:#9370db}#mermaid-svg-ul62DcX7s2CQYwrM .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-ul62DcX7s2CQYwrM .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-ul62DcX7s2CQYwrM .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-ul62DcX7s2CQYwrM .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-ul62DcX7s2CQYwrM .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-ul62DcX7s2CQYwrM .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-ul62DcX7s2CQYwrM .note-edge{stroke-dasharray:5}#mermaid-svg-ul62DcX7s2CQYwrM .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-ul62DcX7s2CQYwrM .error-icon{fill:#522}#mermaid-svg-ul62DcX7s2CQYwrM .error-text{fill:#522;stroke:#522}#mermaid-svg-ul62DcX7s2CQYwrM .edge-thickness-normal{stroke-width:2px}#mermaid-svg-ul62DcX7s2CQYwrM .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-ul62DcX7s2CQYwrM .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-ul62DcX7s2CQYwrM .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-ul62DcX7s2CQYwrM .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-ul62DcX7s2CQYwrM .marker{fill:#333}#mermaid-svg-ul62DcX7s2CQYwrM .marker.cross{stroke:#333} :root { --mermaid-font-family: "trebuchet ms", verdana, arial;} #mermaid-svg-ul62DcX7s2CQYwrM { color: rgba(0, 0, 0, 0.75); font: normal normal normal normal 16px/26px -apple-system, "SF UI Text", Arial, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif; } no yes 程序进入 环绕通知,前段 前置通知 原程序执行 是否有异常 后置通知 环绕通知,后段 程序返回 异常通知 后置通知