Heim > Java > javaLernprogramm > Drei Implementierungsmethoden von SpringAOP

Drei Implementierungsmethoden von SpringAOP

Guanhui
Freigeben: 2020-07-21 13:06:36
nach vorne
2300 Leute haben es durchsucht

Drei Implementierungsmethoden von SpringAOP

Einführung in das AOP-bezogene JAR-Paket

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>5.1.12.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.9.4</version>
</dependency>
Nach dem Login kopieren

TransferServiceImpl.java-Datei:

package com.lagou.edu.service.impl;
import com.lagou.edu.dao.AccountDao;
import com.lagou.edu.pojo.Account;
import com.lagou.edu.service.TransferService;
import com.lagou.edu.utils.ConnectionUtils;
import com.lagou.edu.utils.TransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
/**
 * @author 应癫
 */
@Service("transferService")
public class TransferServiceImpl implements TransferService {
  // 最佳状态
  // @Autowired 按照类型注入 ,如果按照类型无法唯一锁定对象,可以结合@Qualifier指定具体的id
  @Autowired
  @Qualifier("accountDao")
  private AccountDao accountDao;

  @Override
  public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
    /*try{
      // 开启事务(关闭事务的自动提交)
      TransactionManager.getInstance().beginTransaction();*/
      System.out.println("执行转账业务逻辑");
      Account from = accountDao.queryAccountByCardNo(fromCardNo);
      Account to = accountDao.queryAccountByCardNo(toCardNo);
      from.setMoney(from.getMoney()-money);
      to.setMoney(to.getMoney()+money);
      accountDao.updateAccountByCardNo(to);
      //int c = 1/0;
      accountDao.updateAccountByCardNo(from);
  }
}
Nach dem Login kopieren

Druckprotokoll Util:

package com.lagou.edu.utils;

/**
 * @author 应癫
 */

public class LogUtils {

  /**
   * 业务逻辑开始之前执行
   */
  
  public void beforeMethod(JoinPoint joinPoint) {
     Object[] args = joinPoint.getArgs();
    for (int i = 0; i < args.length; i++) {
      Object arg = args[i];
      System.out.println(arg);
    }
    System.out.println("业务逻辑开始执行之前执行.......");
  }


  /**
   * 业务逻辑结束时执行(无论异常与否)
   */

  public void afterMethod() {
    System.out.println("业务逻辑结束时执行,无论异常与否都执行.......");
  }

  /**
   * 异常时时执行
   */
  public void exceptionMethod() {
    System.out.println("异常时执行.......");
  }

  /**
   * 业务逻辑正常时执行
   */

  public void successMethod(Object retVal) {
    System.out.println("业务逻辑正常时执行.......");
  }

}

public Object arroundMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    System.out.println("环绕通知中的beforemethod....");

    Object result = null;
    try{
      // 控制原有业务逻辑是否执行
      // result = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
    }catch(Exception e) {
      System.out.println("环绕通知中的exceptionmethod....");
    }finally {
      System.out.println("环绕通知中的after method....");
    }

    return result;
  }
Nach dem Login kopieren

applicationContext xml

<!--进行aop相关的xml配置,配置aop的过程其实就是把aop相关术语落地-->
  <!--横切逻辑bean-->
<bean id="logUtils" class="com.lagou.edu.utils.LogUtils"></bean>
  <!--使用config标签表明开始aop配置,在内部配置切面aspect-->

  <!--aspect  =  切入点(锁定方法) + 方位点(锁定方法中的特殊时机)+ 横切逻辑 -->
  <aop:config>
    <aop:aspect id="logAspect" ref="logUtils">

      <!--切入点锁定我们感兴趣的方法,使用aspectj语法表达式-->
      <!--..参数中的两个点表示可以有参数,也可以没有参数,有的话也可以是任意类型,参数中的 *表示参数可以是任意类型,但必须有参数。 -->
      <!--包名中的..两个点表示中间可以是任意层-->
      <!--<aop:pointcut id="pt1" expression="execution(* *..*.*(..))"/>-->
     <aop:pointcut id="pt1" expression="execution(public void com.lagou.edu.service.impl.TransferServiceImpl.transfer(java.lang.String,java.lang.String,int))"/>

<!--      <aop:pointcut id="pt1" expression="execution(* com.lagou.edu.service.impl.TransferServiceImpl.*(..))"/>
-->
      <!--方位信息,pointcut-ref关联切入点-->
      <!--aop:before前置通知/增强-->
      <aop:before method="beforeMethod" pointcut-ref="pt1"/>
      <!--aop:after,最终通知,无论如何都执行-->
      <!--aop:after-returnning,正常执行通知,retValue是接受方法的返回值的-->
      <aop:after-returning method="successMethod" returning="retValue"/>
      <!--aop:after-throwing,异常通知-->

      <aop:around method="arroundMethod" pointcut-ref="pt1"/>

    </aop:aspect>
  </aop:config>-->
Nach dem Login kopieren

Test:

 /**
   * 测试xml aop
   */
  @Test
  public void testXmlAop() throws Exception {
    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    TransferService transferService = applicationContext.getBean(TransferService.class);
    transferService.transfer("6029621011000","6029621011001",100);
  }
Nach dem Login kopieren

Surround-Benachrichtigungen werden nicht zusammen mit Vor- und Nachbenachrichtigungen verwendet, da Surround-Benachrichtigungen Vor- und Nachpositionierungsfunktionen implementieren und die ursprüngliche Geschäftslogikausführung steuern können oder nicht, sehr mächtig.

XML+Annotation-Methode

Ändern Sie die obige reine XML-Methode in eine Annotation-Methode
Entfernen Sie den Inhalt in applicationContext.xml und ändern Sie „Hinzufügen“. Anmerkungen zur Klasse:

package com.lagou.edu.utils;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author 应癫
 */
@Component
@Aspect
public class LogUtils {


  @Pointcut("execution(* com.lagou.edu.service.impl.TransferServiceImpl.*(..))")
  public void pt1(){

  }


  /**
   * 业务逻辑开始之前执行
   */
  @Before("pt1()")
  public void beforeMethod(JoinPoint joinPoint) {
    Object[] args = joinPoint.getArgs();
    for (int i = 0; i < args.length; i++) {
      Object arg = args[i];
      System.out.println(arg);
    }
    System.out.println("业务逻辑开始执行之前执行.......");
  }


  /**
   * 业务逻辑结束时执行(无论异常与否)
   */
  @After("pt1()")
  public void afterMethod() {
    System.out.println("业务逻辑结束时执行,无论异常与否都执行.......");
  }


  /**
   * 异常时时执行
   */
  @AfterThrowing("pt1()")
  public void exceptionMethod() {
    System.out.println("异常时执行.......");
  }


  /**
   * 业务逻辑正常时执行
   */
  @AfterReturning(value = "pt1()",returning = "retVal")
  public void successMethod(Object retVal) {
    System.out.println("业务逻辑正常时执行.......");
  }


  /**
   * 环绕通知
   *
   */
  /*@Around("pt1()")*/
  public Object arroundMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    System.out.println("环绕通知中的beforemethod....");

    Object result = null;
    try{
      // 控制原有业务逻辑是否执行
      // result = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
    }catch(Exception e) {
      System.out.println("环绕通知中的exceptionmethod....");
    }finally {
      System.out.println("环绕通知中的after method....");
    }

    return result;
  }

}
Nach dem Login kopieren

Konfigurieren Sie den Anmerkungstreiber in application.xml:

 <!--开启aop注解驱动
    proxy-target-class:true强制使用cglib

  -->
  <aop:aspectj-autoproxy/>
Nach dem Login kopieren
Nach dem Login kopieren

Reiner Anmerkungsmodus

Wir Sie müssen nur den annotationsgesteuerten Teil im XML+Annotation-Modus ersetzen.
Ändern Sie

 <!--开启aop注解驱动
    proxy-target-class:true强制使用cglib

  -->
  <aop:aspectj-autoproxy/>
Nach dem Login kopieren
Nach dem Login kopieren

in @EnableAspectJAutoProxy //Aktivieren Sie die Unterstützung von Spring für Annotation AOP und fügen Sie es zu jedem Projekt hinzu. Gehen Sie einfach zu einer Konfiguration Klasse.

Empfohlenes Tutorial: „Java-Tutorial

Das obige ist der detaillierte Inhalt vonDrei Implementierungsmethoden von SpringAOP. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage