<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] # 簡介 # 準備類 ~~~ public class UserServiceImpl { public Object save(String name) { System.out.println("保存用戶---"); return 1; } } ~~~ # 定義通知 準備的類還是用UserServiceImpl ~~~ package aspect; 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; //通知類 //表示該類是一個通知類 //order指定優先級,值越小優先級越大 @Order(100) @Aspect public class MyAdvice { /** * 聲明切點(對那些方法攔截) */ @Pointcut("execution(* com.spring.service.*ServiceImpl.*(..))") public void pc() { System.out.println("pc方法"); } /** * 前置通知 */ @Before("MyAdvice.pc()") public void before(JoinPoint joinPoint) { System.out.println("---"); //使用前置通知可以完成日志記錄,權限控制 System.out.println("攔截的目標類: " + joinPoint.getSignature().getDeclaringTypeName()); System.out.println("攔截的方法名稱: "+ joinPoint.getSignature().getName()); System.out.println("輸入的參數: " + Arrays.asList(joinPoint.getArgs())); System.out.println("這是前置通知"); } /** * 后置通知(如果出現異常不會調用) */ @Before("MyAdvice.pc()") public void before(JoinPoint joinPoint) { System.out.println("---"); //使用前置通知可以完成日志記錄,權限控制 System.out.println("攔截的目標類: " + joinPoint.getSignature().getDeclaringTypeName()); System.out.println("攔截的方法名稱: "+ joinPoint.getSignature().getName()); System.out.println("輸入的參數: " + Arrays.asList(joinPoint.getArgs())); System.out.println("這是前置通知"); } /** * 后置通知(如果出現異常不會調用) */ @AfterReturning(value = "execution(* com.spring.service.*ServiceImpl.*(..))", returning = "val") public void afterReturning(JoinPoint joinPoint, Object val) { /** * 第二個參數可以獲取目標方法的返回值 * 注意:需要在配置文件中配置 returning="val" */ System.out.println("---"); System.out.println("返回值: " + val); System.out.println("這是后置通知(如果出現異常不會調用)"); } /** * 環繞通知 */ @Around("execution(* com.spring.service.*ServiceImpl.*(..))") public Object around(ProceedingJoinPoint pjp) throws Throwable { //獲取方法名 String methodName = pjp.getSignature().getName(); //獲取參數 Object[] args = pjp.getArgs(); Object result = null; try { //前置通知 System.out.println("這是環繞通知之前的方法: " + methodName + " 開始,參數: " + Arrays.asList(args)); //執行方法 result = pjp.proceed(); //返回通知 System.out.println("方法: " + methodName + " 返回 " + result); } catch (Throwable e) { //異常通知 System.out.println("方法: " + methodName + " 異常 " + e); } finally { //后置通知 System.out.println("這是環繞通知之后的: " + methodName + " 結束"); } return result; } /** * 異常通知 */ @AfterThrowing(pointcut = "MyAdvice.pc()") public void afterException() { System.out.println("出現異常了: "); } /** * 后置通知(如果出現異常也會調用) */ @After("execution(* com.spring.service.*ServiceImpl.*(..))") public void after(JoinPoint joinPoint) { System.out.println("---"); System.out.println("攔截的方法名稱: " + joinPoint.getSignature().getName()); System.out.println("這是后置通知(如果出現異常也會調用)"); } } ~~~ # 進行配置 ~~~ <aop:config>來聲明要對aop進行配置 <aop:pointcut>它是用于聲明切點(簡單說就是對哪些方法進行攔截) <aop:advisor> 定義傳統的aop的切面,傳統的aop切面它只能包含一個切點與一個增強 <aop:aspect>定義aspectj框架的切面.,它可以包含多個切點與多個通知 ~~~ ~~~ <!-- 準備工作: 導入aop(約束)命名空間 --> <!-- 1.配置目標對象 --> <bean name="userService" class="service.UserServiceImpl"></bean> <!-- 2.配置通知對象 --> <bean name="myAdvice" class="aspect.MyAdvice"></bean> <!-- 3.配置將通知織入目標對象 --> <aop:config> <!-- 配置切入點 public void service.UserServiceImpl.save() void service.UserServiceImpl.save() * service.UserServiceImpl.save() * service.UserServiceImpl.*() * service.UserServiceImpl.*(..) * service.*ServiceImpl.*(..) * service..*ServiceImpl.*(..) //還會找子包 --> <aop:pointcut expression="execution(* service.*ServiceImpl.*(..))" id="pc"/> <aop:aspect ref="myAdvice"> <!-- 指定名為before方法為前置通知 --> <aop:before method="before" pointcut-ref="pc" /> <!-- 后置通知(如果出現異常不會調用) --> <aop:after-returning method="afterReturning" pointcut-ref="pc" returning="val" /> <!-- 環繞通知 --> <aop:around method="around" pointcut-ref="pc" /> <!-- 異常攔截通知 --> <aop:after-throwing method="afterException" pointcut-ref="pc" /> <!-- 后置 --> <aop:after method="after" pointcut-ref="pc" /> </aop:aspect> </aop:config> </beans> ~~~ ~~~ 關于execution語法常用: 1. execution(public * *()) 所有的public的方法 2. execution(* cn.study.aop.*(..)) 所有的aop包下的所有類的方法(不包含子包) 3. execution(* cn.study.aop..*(..)) 所有的aop包及其子包下的所有類的方法 4. execution(* cn.study.aop.IOrderService.*(..)) IOrderService接口中定義的所有方法 5. execution(* cn.study.aop.IOrderService+.*(..)) 匹配實現特定接口所有類的方法 6. execution(* save*(..)) 區配所有的以save開頭的方法 ~~~ # 測試 ~~~ //幫我們創建容器 @RunWith(SpringJUnit4ClassRunner.class) // 指定創建容器時使用那個配置文件 @ContextConfiguration("classpath:applicationContext.xml") public class Demo { @Resource(name = "userService") private UserService us; @Test public void fun1() { us.save("hello"); } } ~~~ 結果 ~~~ 這是環繞通知之前的部分 --- 攔截的目標類: com.spring.service.UserServiceImpl 攔截的方法名稱: save 輸入的參數: [hello] 這是前置通知 保存用戶--- 這是環繞通知之后的部分 --- 攔截的方法名稱: save 這是后置通知(如果出現異常也會調用) --- 返回值: 1 這是后置通知(如果出現異常不會調用) ~~~
                  <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>

                              哎呀哎呀视频在线观看