Springは、注釈付きの単純なAOPデモを実装します

IoCに関連する基本的なコンテンツは終了しました。今回は、Springの2番目の機能であるAOP、アスペクト指向プログラミングを紹介します。用語は理解しにくいように聞こえますが、問題ではありません。すべてが例に含まれています。簡単な例を見てみましょう。理解する。

最初に全体的な構造を見てください

その中にはいくつかのカテゴリーがあります、

まず、pom.xmlは内部のjarを参照する必要があります。

2番目:インターフェイスを実装するためのインターフェイスHellointerfaceを定義します:UserServiceImpl

3番目:root-context.xmlを追加します

4番目:AopクラスTimeMonitorを追加します

5番目:springaopapplicationテストクラス 

最初のステップはインターフェースを作成することです。これは通常のクラスであり、aopの考え方とは関係ありません。

インターフェイスHelloInterfaceを作成します

package springaop.aopservice;

public interface HelloInterface {
    void sayHello();
}

ステップ2:実装クラスUserServiceImplを作成します

package springaop.aopservice.impl;

import org.springframework.stereotype.Service;
import springaop.aopservice.HelloInterface;

@Service
public class UserServiceImpl  implements HelloInterface {
    public void sayHello() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("userHello");
    }
}

ステップ3:root-context.xmlを作成する

<?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-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    <context:component-scan base-package="springaop"></context:component-scan>

</beans>

ステップ4:pom.xmlを追加する 

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.9</version>
</dependency>

ステップ5:aopクラスを追加する

ここでは、aopの5つの注釈を理解する必要があります

 @after通知メソッドは、ターゲットメソッドが例外を返すかスローした後に呼び出されます

@afterreturning通知メソッドは、戻った後に呼び出されます

@AfterThrowing通知メソッドは、例外をスローした後に呼び出されます

@Around通知メソッドは、ターゲットメソッドをカプセル化します。つまり、メソッドの前後の両方で実行されます。Object re = pjp.proceed();を使用して、前後を区別します

@Before通知メソッドは、ターゲットメソッドが呼び出される前に実行されます

package springaop.aopservice.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Service;

@Service
@Aspect
public class TimeMonitor {

    @Pointcut("execution(* springaop.aopservice.impl.UserServiceImpl.sayHello(..))")
    public  void  performance(){}

    @Around("performance()")
    public void monitorAround(ProceedingJoinPoint pjp) throws Throwable {

        System.out.println("method start time:" + System.currentTimeMillis());

        Object re = pjp.proceed();

        System.out.println("method end time:" + System.currentTimeMillis());

    }


    @Before("performance()")
    public  void  tekeSeats(){

        System.out.println("我来测试一下,看看能不能先执行method end time:" + System.currentTimeMillis());
    }

    @AfterReturning("performance()")
    public  void  afterSeats(){

        System.out.println("我来测试一下,看看能不能后执行method end time:" + System.currentTimeMillis());
    }

}

6番目のステップ:

テスト

package springaop.springaop;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import springaop.aopservice.HelloInterface;

@SpringBootTest
class SpringaopApplicationTests {

    ///通过XMl的方式实现 aop
    @Test
    void contextLoads() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("root-context.xml");
        HelloInterface userService = context.getBean(HelloInterface.class);
        userService.sayHello();
        context.close();
    }


}

 

 

おすすめ

転載: blog.csdn.net/xulong5000/article/details/107788341