Spring Learning_AOP_Notice

AOP

Why use AOP?
There are three classes, each of which has an add Student() method. If we need to check whether the student we want to add already exists in the database before executing addStudent(), we need to separately Adding the check() method before this method is very troublesome, isn't it?
so...
With AOP (of course, it's far more than this function), you can specify who, the virtual machine, you call check() for me every time you run addStudent() to check it, haha, hard-working virtual machine .
Simple pre-notification
1, xml configuration

<!--    配置前置通知-->
<!--    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>

Examples of expresstion expressions:
1. The execution of any public method:
execution(public * (…))
2. The
execution of any method starting with "set": execution( set*(…))
3.
Any of the AccountService interface Method execution:
execution(* com.xyz.service.AccountService. (…))
4. Execution of any method defined in the service package:
execution(
com.xyz.service .. (…))
5. Defined in service The
execution of any method of any class in the package and all sub-packages: execution(* com.xyz.service… . (…))
6. The
execution of any method of the JoinPointObjP2 class defined in the pointcutexp package and all sub-packages: execution (* com.test.spring.aop.pointcutexp…JoinPointObjP2. (…))") The
closest (…) is the method name, and the closest.
(…)) is the class name or interface name

2. The pre-notification class
LogBefore.java implements the MethodBeforeAdvice interface

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

The post notification will not be written, the same is the same, in a hurry.
Exception notification :
Configure exception notification in the container

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

Exception notification class, note that we did not prompt us which method to implement when we implemented the interface, but we still have to implement the following methods:

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());
    }
}

After deliberately adding a human to make personDao a null pointer, execute:
Insert picture description here
surround notification : it is the most powerful notification that can be notified anywhere and can get full control of the target method.
New surround class: 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;
    }
}

Insert picture description here

May your heart be like flowers and trees

Guess you like

Origin blog.csdn.net/nbcsdn/article/details/99424449