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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## 步驟 1. 導入AOP約束(命名空間). 2. 配置目標對象. 3. 配置通知對象. 4. 配置將通知織入目標對象. ## 步驟 ``` ~~~ <!-- spring 中基于XML的AOP配置步驟 1.把通知Bean也交給spring來管理. 2.使用aop:config標簽表名開始AOP的配置. 3.使用aop:aspect標簽表名開始AOP的配置. id屬性:是給切面提供一個唯一標識. ref屬性:是指定通知類bean的ID. 4.在aop:aspect標簽的內部使用對應標簽來配置通知的類型. 我們現在示例是讓通知類的方法在切入點方法執行之前執行,所以是前置通知. aop:before :表示配置前置通知. method屬性:用于指定通知類中哪個方法是前置通知. pointcut屬性:用于指定切入點表達式,該表達式的含義指的是對業務層中哪些方法增強 --> ~~~ ``` ## 代碼 目標類: ~~~ public class UserServiceImpl implements UserService { @Override public void save() { System.out.println("save"); } @Override public void delete() { System.out.println("delete"); } @Override public void update() { System.out.println("update"); } @Override public void get() { System.out.println("get"); } @Override public String toString() { return "UserServiceImpl{}"; } } ~~~ 通知類: ~~~ public class MyAdvice { //前置通知:目標方法運行之前調用 public void before() { System.out.println("前置通知"); } //后置通知:如果出現異常不會調用 public void afterRunning() { System.out.println("后置通知,異常不調用"); } //環繞通知:在目標方法之前和之后都調用 public Object around(ProceedingJoinPoint pjp) throws Throwable { System.out.println("環繞通知之前"); //調用目標方法 Object proceed = pjp.proceed(); System.out.println("環繞通知之后"); return proceed; } //異常攔截通知:如果出現異常就會調用 public void afterException() { System.out.println("異常通知"); } //后置通知:無論是否出現異常都會調用 public void after() { System.out.println("后置通知,異常調用"); } } ~~~ 配置文件: ~~~ <?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--配置目標對象--> <bean name="userServiceTarget" class="com.like.serviceImpl.UserServiceImpl"/> <!--配置通知對象--> <bean name="myAdvice" class="com.like.aspect.MyAdvice"/> <aop:config> <!--配置切入點 public void com.like.serviceImpl.UserServiceImpl.save() //完整寫法 void com.like.serviceImpl.UserServiceImpl.save() //省略public * com.like.serviceImpl.UserServiceImpl.save() //不對返回值做要求 * com.like.serviceImpl.UserServiceImpl.*() //所有方法,必須是空參 * com.like.serviceImpl.UserServiceImpl.*(..) //所有方法,任意參數,可以沒參數 * ..UserServiceImpl.*(..) //當前包及其子包 * com.like.serviceImpl.*ServiceImpl.*(..) //所有方法,ServiceImpl后綴的類 * com.like.serviceImpl.*ServiceImpl.*(int) //指定int類型 * com.like.serviceImpl.*ServiceImpl.*(*) //任意類型,但是必須有參數 * com.like.serviceImpl..*ServiceImpl.*(..) //所有方法,ServiceImpl后綴的類還有它的子包 --> <aop:pointcut id="pc" expression="execution( * com.like.serviceImpl.*ServiceImpl.*(..))"/> <!--配置切面, id是切面的唯一標識.ref是通知類的引用--> <aop:aspect id="myAdvicexxxx" ref="myAdvice"> <!--指定名為before方法作為前置通知--> <aop:before method="before" pointcut-ref="pc"/> //這里是共用一個切入點了,如果使用ponitcut屬性可以單獨制定切入點 <!--指定名為afterRunning方法作為后置通知--> <aop:after-returning method="afterRunning" pointcut-ref="pc"/> <!--指定名為around方法作為環繞通知--> <aop:around method="around" pointcut-ref="pc"/> <!--指定名為afterException方法作為異常通知--> <aop:after-throwing method="afterException" pointcut-ref="pc"/> <!--指定名為after方法作為最終通知(有異常也通知)--> <aop:after method="after" pointcut-ref="pc"/> </aop:aspect> </aop:config> </beans> ~~~ 調用: ~~~ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService) context.getBean("userServiceTarget"); //如果符合要求,獲取到的就是代理對象.是無法獲取被代理對象的 System.out.println(userService); ~~~ ## 通用化切入點表達式 所有切面都能使用. 但是必須在所有切面之前. ~~~ <aop:config> <aop:pointcut id="advice" expression="execution(* com.like.service.impl.*ServiceImpl.*(..))"/> <aop:aspect ref="logger"> <aop:around method="around" pointcut-ref="advice"/> </aop:aspect> </aop:config> ~~~ ## 單個切面使用 ~~~ <aop:config> <aop:aspect ref="logger"> <aop:pointcut id="ad1" expression="execution(* com.like.service.impl.*ServiceImpl.*(..))"/> <aop:around method="around" pointcut-ref="ad1"/> </aop:aspect> </aop:config> ~~~ ## 單個方法切入點表達式 ~~~ <aop:config> <aop:aspect ref="logger"> <aop:around method="around" pointcut="execution(* com.like.service.impl.*ServiceImpl.*(..))"/> </aop:aspect> </aop:config> ~~~ ## 注解方式 ~~~ <?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--配置目標對象--> <bean id="userServiceTarget" class="com.like.serviceImpl.UserServiceImpl"/> <!--配置將通知織入目標對象--> <bean id="myAdvice" class="com.like.aspect.MyAdvice"/> <!--開啟使用注解完成織入--> <aop:aspectj-autoproxy/> </beans> ~~~ ~~~ package com.like.aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; //用該注解表示是通知類 @Aspect public class MyAdvice { @Pointcut("execution( * com.like.serviceImpl.*ServiceImpl.*(..))") public void pc(){} //指定該方法是前置通知,并指定切入點 @Before("execution( * com.like.serviceImpl.*ServiceImpl.*(..))") public void before() { System.out.println("前置通知"); } //后置通知:如果出現異常不會調用 @AfterReturning("MyAdvice.pc()") //這是簡寫方法,這樣講就不用為每個通知都指定范圍了 public void afterRunning() { System.out.println("后置通知,異常不調用"); } //環繞通知:在目標方法之前和之后都調用 @Around("execution( * com.like.serviceImpl.*ServiceImpl.*(..))") public Object around(ProceedingJoinPoint pjp) throws Throwable { System.out.println("環繞通知之前"); //調用目標方法 Object proceed = pjp.proceed(); System.out.println("環繞通知之后"); return proceed; } //異常攔截通知:如果出現異常就會調用 @AfterThrowing("execution( * com.like.serviceImpl.*ServiceImpl.*(..))") public void afterException() { System.out.println("異常通知"); } //最終通知:無論是否出現異常都會調用 @After("execution( * com.like.serviceImpl.*ServiceImpl.*(..))") public void after() { System.out.println("后置通知,異常調用"); } } ~~~ ## 注意 后置通知和一場通知最終只能執行一個.
                  <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>

                              哎呀哎呀视频在线观看