Spring Learning_AOP_Notice

AOP

AOPを使用する理由
3つのクラスがあり、それぞれにadd Student()メソッドがあります。addStudent()を実行する前に、追加する生徒がデータベースに既に存在するかどうかを確認する必要がある場合は、個別に行う必要がありますこのメソッドの前にcheck()メソッドを追加するのは非常に面倒ですよね。
そう...
ではAOP、あなたは誰、仮想マシンを指定することができます(もちろん、それははるかにこの関数よりもだ)、あなたは、懸命に働いた仮想マシンを、ハハ、それをチェックするために私のためにあなたがaddStudentを(実行するたびに)チェック()を呼び出します。
簡単な事前通知
1、xml構成

<!--    配置前置通知-->
<!--    addPerson所在方法-->
    <bean id="personServiceImpl" class="com.itheima.service.PersonServiceImpl">
        <property name="personDao" ref="personDao">

        </property>
    </bean>
<!--    前置通知类-->
    <bean id="logBefore" class="com.itheima.aop.LogBefore">

    </bean>
<!--    关联-->
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(public void com.itheima.service.PersonServiceImpl.deletePerson(int)) or execution(public void com.itheima.service.PersonServiceImpl.addPerson(com.itheima.entity.Person))"/>
        <aop:advisor advice-ref="logBefore" pointcut-ref="pointcut"/>
    </aop:config>

表現式の例:
1.任意のパブリックメソッドの
実行execution(public * (…))
2.「set」で始まる任意のメソッドの
実行execution( set *(…))
3.
任意のAccountService インターフェイスメソッドの実行:
execution(* com.xyz.service.AccountService。(…))
4.サービスパッケージで定義されたメソッドの
実行execution(
com.xyz.service ..(…))
5.サービスで定義パッケージおよびすべてのサブパッケージ内の任意のクラスの任意のメソッドの
実行execution(* com.xyz.service… (…))
6. pointcutexpパッケージおよびすべてのサブパッケージで定義されたJoinPointObjP2クラスの任意のメソッドの
実行実行(* com.test.spring.aop.pointcutexp…JoinPointObjP2。(…)) ")
最も近い(…)はメソッド名であり、最も近い
(…))はクラス名またはインターフェース名です

2.事前通知クラス
LogBefore.javaは、MethodBeforeAdviceインターフェースを実装します

public class LogBefore implements MethodBeforeAdvice {
    
    
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
    
    
        System.out.println("前置通知!");
    }
}

急いで投稿通知は書かれません、同じは同じです。
例外通知
コンテナで例外通知を構成します

   <bean id="logException" class="com.itheima.aop.LogException"/>
<!--    异常通知-->
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(public void com.itheima.service.PersonServiceImpl.deletePerson(int)) or execution(public void com.itheima.service.PersonServiceImpl.addPerson(com.itheima.entity.Person))"/>
        <aop:advisor advice-ref="logException" pointcut-ref="pointcut"/>
    </aop:config>

例外通知クラス。インターフェイスを実装するときに実装するメソッドを指定しなかったが、次のメソッドを実装する必要があることに注意してください。

package com.itheima.aop;
import org.springframework.aop.ThrowsAdvice;
import java.lang.reflect.Method;
public class LogException implements ThrowsAdvice {
    
    
    public void afterThrowing(Method method, Object[] args, Object target, Throwable throwable){
    
    
        System.out.println("异常通知:目标对象," + target + "方法名," + method.getName() + "方法的参数个数:" + args.length + "异常类型:" + throwable.getMessage());
    }
}

personDaoをnullポインターにするために意図的に人間を追加した後、次のコマンドを実行します。
ここに画像の説明を挿入
サラウンド通知:どこでも通知でき、ターゲットメソッドを完全に制御できる最も強力な通知です。
新しいサラウンドクラス:LogArround.java

package com.itheima.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class LogArround implements MethodInterceptor {
    
    
    @Override
    public Object invoke(MethodInvocation invocation){
    
    
        Object result = null;
        try {
    
    
            //  前置通知
            System.out.println("环绕前置");
            result = invocation.proceed();//控制目标方法的执行,前面是前置通知,后面是后置通知
            //后置通知
            System.out.print("环绕后置通知:");
            System.out.println("目标对象," + invocation.getThis() + "方法名," + invocation.getMethod().getName() + "方法的参数个数:" + invocation.getArguments().length + "返回参数:" + result);
        } catch (Throwable throwable) {
    
    
            //环绕异常通知
            System.out.println("环绕异常通知");
        }
        return result;
    }
}

ここに画像の説明を挿入

心が花や木のように

おすすめ

転載: blog.csdn.net/nbcsdn/article/details/99424449