<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                - AspectJ:Java 社區里最完整最流行的 AOP 框架. - 在 Spring2.0 以上版本中, 可以使用基于 AspectJ 注解或基于 XML 配置的 AOP ### 使用步驟: 1.aopalliance.jar、aspectj.weaver.jar 和 spring-aspects.jar 2.spring配置文件中添加:xmlns:aop="http://www.springframework.org/schema/aop" 3.要在 Spring IOC 容器中啟用 AspectJ 注解支持, 只要在 Bean 配置文件中定義一個空的 XML 元素 aop:aspectj-autoproxy 。使AspectJ相關注解生效:當調用目標方法,跟Aspect中聲明的方法相匹配的時候,AOP框架會自動的為目標方法所在的類創建代理對象 >#### 1.基于注解的方式實現springAOP: 1.創建切面類(LoggingAspect)。 @Aspect:聲明當前類為切面類。 @Component:交給springIOC容器進行管理。 @Before:前置通知。 需要使用切點表達式:@Before("execution(* cn.li.service.impl.*.*(..))"),/** * 可以使用AspectJ表達式,對目標方法進行抽象概括。 * * execution(* cn.li.service.impl.*.*(String,..)) * * 第一個* 表示匹配所有訪問修飾符 以及所有返回值類型的方法 第二個* 表示當前包下所有的類 第三個* 表示所有的方法名稱 .. * 表示匹配任意多個參數。 (String,.. )表示匹配第一個參數為String類型的方法,..表示匹配任意數量任意類型的參數。 * String,String 表示匹配參數為兩個字符串的方法。 */其中(Joinpoint)對象中封裝了目標方法的一些信息,例如獲取目標方法名稱,獲取目標方法參數等。 `@Order(number)`:@Order 表示 配置多個切面之間的優先級問題 。 誰的值越小誰的優先級越高 。 `@After`:后置通知。使用方法與前置通知相同。注意:后置通知即使方法異常也會成功執行,但是后置通知無法拿到目標方法的返回結果。需要返回通知。 `@AfterReturnning`:返回通知,在方法正常之后之后執行的通知,可以拿到目標方法的返回結果。使用返回通知需要注意的是:指定returnning="result",afterReturnningAdvice(JoinPoint joinpoint,Object result)與方法入參位置的對象名稱一致,否則會產生異常。 `@AfterThrowing`:異常通知,方法產生異常的時候,可以拿到異常信息。同樣需要注意的是:指定throwing="e",與afterThrowingAdvice(JoinPoint joinpoint,Exception e)方法入參位置的異常對象名稱一致。 `@Around:`環繞通知 ~~~ 具體代碼實現如下:》》》》》 //當前類就是一個切面類 //想要一個類成為切面類,1.添加@Component 注釋標注 當前類被springIOC容器所管理 //2.@Aspect表示當前類為一個切面類 //@Order 表示 配置多個切面之間的優先級問題 。 誰的值越小誰的優先級越高 。 //@Order(2) //@Aspect //@Component public class LoggingAspect { // @Before表示前置通知。指的是在特定位置之前,去執行該方法 // 通知 其實是切面類中一個具體的方法 // JoinPoint 表示連接點 /** * 可以使用AspectJ表達式,對目標方法進行抽象概括。 * * execution(* cn.li.service.impl.*.*(String,..)) * * 第一個* 表示匹配所有訪問修飾符 以及所有返回值類型的方法 第二個* 表示當前包下所有的類 第三個* 表示所有的方法名稱 .. * 表示匹配任意多個參數。 (String,.. )表示匹配第一個參數為String類型的方法,..表示匹配任意數量任意類型的參數。 * String,String 表示匹配參數為兩個字符串的方法。 */ //@Pointcut("execution(* cn.li.service.impl.*.*(..))") public void declareRepeatJoinPointExpression(){ } //@Before("execution(* cn.li.service.impl.*.*(..))") public void beforeLog(JoinPoint joinpoint) { // 通過連接點對象可以獲得調用目標方法的名稱和參數 // 獲得方法名稱。能拿到你要調用方法的名稱 String method = joinpoint.getSignature().getName(); // 獲得調用方法時傳遞的參數 List arguments = Arrays.asList(joinpoint.getArgs()); System.out.println("前置日志調用了方法" + method + "方法,參數是" + arguments); } // 注意:后置通知即使方法異常也會成功執行,但是后置通知無法拿到目標方法的返回結果。 需要使用返回通知。。。 //@After("execution(* cn.li.service.impl.*.*(..))") public void afterLog(JoinPoint joinpoint) { String method = joinpoint.getSignature().getName(); List arguments = Arrays.asList(joinpoint.getArgs()); System.out.println("后置日志 。"); } // 返回通知 // 注意:返回通知 ,其實跟后置通知一樣 。都是在目標方法執行完之后 才會被執行 。 // returning="result" 名字 要跟參數列表中 Object 對象的名稱一致 ,不然產生異常。 //@AfterReturning(value = "execution(* cn.li.service.impl.*.*(..))", returning = "result") public void testAfterReturning(JoinPoint joinpoint, Object result) { String method = joinpoint.getSignature().getName(); System.out.println("我是返回通知 。 我在目標方法核心業務執行完才會執行 。" + result); } //@AfterThrowing(value="execution(* cn.li.service.impl.*.count(..))",throwing="e") public void testAfterThrowing(JoinPoint joinpoint,Exception e){ System.out.println("我是異常通知 ,我是在方法產生異常后執行的。"+e); } //環繞通知 。 跟動態代理的代碼很像。 //@Around("declareRepeatJoinPointExpression()") // public void around(ProceedingJoinPoint pjp){ // //聲明一個Object 對象 用來表示 目標方法的返回值 。 // Object result=null; // String method=pjp.getSignature().getName(); // try { // System.out.println("我是前置日志 。。。"+method); // result = pjp.proceed();//調用proceed() 表示執行被代理類的目標方法。 // System.out.println("我是返回通知"+method+result); // } catch (Throwable e) { // //Throwable 所有異常類跟錯誤類的父類 。Exception Error ... // // TODO Auto-generated catch block // e.printStackTrace(); // System.out.println("異常通知,產生異常的時候 會執行catch 里面的代碼 。"); // } // System.out.println("我是后置通知 。。。"+result+method); // } } ~~~ ~~~ //@Order(1) //@Aspect //@Component public class CheckAspect { //@Before("execution(* cn.li.service.impl.*.*(..))") public void checkBeforeLog(JoinPoint joinpoint){ System.out.println("我是驗證切面的前置通知 。"); } } ~~~ >#### 2.基于XML文件的方式實現springAOP: ~~~ <!-- @Aspect 注解生效 。 讓注解生效,切面中的注解生效。 當調用目標方法,跟Aspect中聲明的方法相匹配的時候, AOP框架會自動的為目標方法所在的類創建代理對象。 作用是讓注解生效 ,當調用的方法,跟通知中聲明的方法一致的時候。AOP框架會自動的為那個方法所在的類生成代理對象,然后在調用目標方法(之前或者之后)把通知中的方法加進去。 --> <!-- <aop:aspectj-autoproxy></aop:aspectj-autoproxy> --> <!-- 配置切面 的bean --> <bean id="checkAspect" class="cn.li.aspect.CheckAspect"></bean> <bean id="loggingAspect" class="cn.li.aspect.LoggingAspect"></bean> <!-- 配置aop --> <aop:config> <!-- 配置切點表達式 --> <aop:pointcut expression="execution(* cn.li.service.impl.*.*(..))" id="pointcut"/> <aop:aspect ref="checkAspect" order="1"> <aop:before method="checkBeforeLog" pointcut-ref="pointcut"/> </aop:aspect> <aop:aspect ref="loggingAspect" order="2"> <aop:before method="beforeLog" pointcut-ref="pointcut"/> <aop:after method="afterLog" pointcut-ref="pointcut"/> <aop:after-returning method="testAfterReturning" returning="result" pointcut-ref="pointcut"/> <aop:after-throwing method="testAfterThrowing" throwing="e" pointcut-ref="pointcut"/> <!-- <aop:around method="around" pointcut-ref="pointcut"/> --> </aop:aspect> </aop:config> ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看