20200114 - spring aop xml-based configuration

Aop configuration file in the configuration bean.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: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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>

Create a accountService the bean to manage
in the creation of a logger let bean to manage.
Our aim is a accountService when each method is called, you can implement this method logger
and then configure a aop


    <aop:config>
        <aop:aspect id="logAdvice" ref="logger">
            <aop:before method="printLog"></aop:before>
        </aop:aspect>
    </aop:config>

This, is not over, because there is no contact logger with accountService
use pointcut = execution (expression) so we can have a relationship.

<aop:before method="printLog" pointcut="execution(public void com.itheima.service.impl.AccoutServiceImpl.saveAccout())"></aop:before>

Test category

public class AOPTest {
    public static void main(String[] args) {
        ApplicationContext  applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        IAccoutService iAccoutService = (IAccoutService)applicationContext.getBean("accoutService");
        iAccoutService.saveAccout();
    }
}

The first step: introducing spring containers
Step: Get the object from the container accountService
third step: performing the method

Here Insert Picture Description
Configuring the ok

The expression for writing
the access modifier can be omitted
return value can use wildcard characters, the return value is an arbitrary
package name can use wildcards denote any package
with a few upgrade package, you need to write a few *
... contain all of the following packages and package
the class and method names are You can use wildcard

Actual development pointcut expressions usually written:
cut all business layer method in implementation class

<!-- 配置前置通知:在切入点方法执行之前执行
            <aop:before method="beforePrintLog" pointcut-ref="pt1" ></aop:before>-->

            <!-- 配置后置通知:在切入点方法正常执行之后值。它和异常通知永远只能执行一个
            <aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt1"></aop:after-returning>-->

            <!-- 配置异常通知:在切入点方法执行产生异常之后执行。它和后置通知永远只能执行一个
            <aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"></aop:after-throwing>-->

            <!-- 配置最终通知:无论切入点方法是否正常执行它都会在其后面执行
            <aop:after method="afterPrintLog" pointcut-ref="pt1"></aop:after>-->

            <!-- 配置环绕通知 详细的注释请看Logger类中-->
            <aop:around method="aroundPringLog" pointcut-ref="pt1"></aop:around>

A statement pt1, you can save

When we configured the around advice, the entry point method is not performed, the notification method executed.
Analysis: Dynamic proxy approach by comparing the around advice, found that around advice in a clear dynamic proxy service layer method calls, and our code does not
solve: provide an interface, proceedingJoinPoint This interface has a method, this method is equivalent to entry point method, the interface can be notified as a parameter surrounded, during program execution, spring framework provides the interface implementation class for us

 public Object aroundPringLog(ProceedingJoinPoint pjp){
        Object rtValue = null;
        try{
            Object[] args = pjp.getArgs();//得到方法执行所需的参数

            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。前置");

            rtValue = pjp.proceed(args);//明确调用业务层方法(切入点方法)

            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。后置");

            return rtValue;
        }catch (Throwable t){
            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。异常");
            throw new RuntimeException(t);
        }finally {
            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。最终");
        }
    }
Published 657 original articles · won praise 39 · views 60000 +

Guess you like

Origin blog.csdn.net/qq_36344771/article/details/103971074