springAOPの3つの実現方式


springAOPの実現方式
三つの純粋なXML方式、XML+注釈、純粋な注釈方式.
Spring AOP思想使用を実現するのは動的代理技術のデフォルトの場合、Springは被代理者が接続を実現するかどうかによってJDKまたはCGLOIBを使用するかを選択します.プロキシオブジェクトが実装されていない場合
どんな接続でもSpringはCGLOIBを選択します.代理先に接続が実現されると、SpringはJDK担当者の代理技術を選択しますが、
私たちは配置の構式を通じて、SpringにCGLOIBを強制的に使用させることができます.
次にaopの実現を開始します.必要なのは、横方向の論理コードは印刷誌で、プロジェクトの標識構法の特定の位置に印刷志の論理を編入したいです.
純粋XML方式
aop関連のjarパッケージを導入します.
<dependency>
   <groupId>org.springframeworkgroupId>
   <artifactId>spring-aopartifactId>
   <version>5.1.12.RELEASEversion>
dependency>

<dependency>
   <groupId>org.aspectjgroupId>
   <artifactId>aspectjweaverartifactId>
   <version>1.9.4version>
dependency>
TransferServiceImpl.javaファイル:
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);



    }
}

印刷ログ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;
    }

appication Contect.xml

    
<bean id="logUtils" class="com.lagou.edu.utils.LogUtils">bean>
   

   
    <aop:config>
        <aop:aspect id="logAspect" ref="logUtils">

           
           
           
           
         <aop:pointcut id="pt1" expression="execution(public void com.lagou.edu.service.impl.TransferServiceImpl.transfer(java.lang.String,java.lang.String,int))"/>


           
           
            <aop:before method="beforeMethod" pointcut-ref="pt1"/>
           
           
            <aop:after-returning method="successMethod" returning="retValue"/>
           

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

        aop:aspect>
    aop:config>-->

テスト:
    /**
     *   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);
    }

サラウンド通知は、フロントとバックの通知と一緒に使用しません.サラウンド通知は、フロントとバックの機能を実現し、既存のトラフィックロジックを制御することができますので、非常に強力です.
XML+コメント方式
上の純粋なXML方式を注釈方式に変えて、aplication Contactext.xmlの内容を削除して、クラスに注釈を追加します.
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;
    }

}

注解ドライバをappication.xmlに設定します.
    
    <aop:aspectj-autoproxy/>
純粋な注釈モード
私達はxml+注解モードの注解駆動部分を交替するだけでいいです.
    
    <aop:aspectj-autoproxy/>
@EnbaleAspect JAtoxy//springを開いてAOPに対する注釈に対する支持を持って、プロジェクトの中で任意の配置類に追加すればいいです.
訪問歓迎:
WeChat公衆番号(プログラマ資料ステーション):code_ダタ