spring Learning 4: aop learning

spring Learning 4: aop learning

A, aop Overview

aop i.e. Oriented Programming, is to simply repeat the program code extracted, you need to perform, the use of dynamic proxy technology without changing the source code of the dynamic enhancement original function.

Role and Benefits: During the program run, does not modify the source code to enhance the existing methods

Two, aop related terms

2.1 Joinpoint (connection point)

Refers to the midpoint method aop, Joinpoint method refers to the individual business layer

2.2 Pointcut (entry point)

It refers to intercept those business layer method

2.3 Advice (notification)

The so-called notice refers to Joinpoint after intercepting the thing to do is to inform.

Type of notification: notification front, rear notification, abnormality notification, the final notification, the notification surround

2.4 Aspect (cut)

Binding is the entry point and the notification (introducing) of

Third, based on the use of aop xml configuration steps

3.1 Extraction notification code into a common class

3.2 notification class vessel arranged to spring

3.3 aop: config to declare aop configuration

3.4 aop: aspect to declare a configuration section

Aop internal use in section 3.5: pointcut to declare entry points

Starting point is to specify which service method will be aop intercept

Writing pointcut expressions:
Keywords: execution (expression)
expression:
access modifiers return value package names package names package name ... the name of the class method name (parameter list)...
Criteria expression written:
public void com.lyy.service.impl.AccountServiceImpl.saveAccount()
Access modifiers may be omitted
void com.lyy.service.impl.AccountServiceImpl.saveAccount()
return value can use wildcards, the return value indicates any
* com.lyy.service.impl.AccountServiceImpl.saveAccount()
package name can use wildcards, it indicates any package. But there are a few levels pack, you need to write a few .
* *.*.*.*.AccountServiceImpl.saveAccount())
Package name can be used .. represent the current package and its subpackages
* *..AccountServiceImpl.saveAccount()
class and method names can be used
to implement wildcard
* *..*.*()
parameter list:
can directly write data types:
basic types of direct write name int
reference type ways to write the package name of the class name java.lang.String.
You can use wildcard represents any type, but must have parameters
you can use .. parameters can indicate whether there can be any type of parameter
all-pass with the wording:
* *..*.*(..)

3.6 Configure the type of notification within the section of

There are pre-notification (aop: before), after advice (aop: after-returning), abnormality notification (after-throwing), the final notification (aop: after), around advice (aop: around)

As result of the configuration

<!--通知类使用注解配置-->
    <!--使用aop:config 来声明aop的配置-->
    <aop:config>
        <aop:aspect id="transactionAspect" ref="transactionManager">
            <!--这个切入点表达式表示com.lyy.service及其子包中的任意类的任意方法-->
            <aop:pointcut id="servicePointcut" expression="execution(* com.lyy.service..*.*(..))"/>
            <!--在切面的内部配置通知的类型-->
            <!--配置前置通知-->
            <aop:before method="startTransaction" pointcut-ref="servicePointcut"/>
            <!--后置通知-->
            <aop:after-returning method="commit" pointcut-ref="servicePointcut"/>
            <!--配置异常通知-->
            <aop:after-throwing method="rollBack" pointcut-ref="servicePointcut"/>
        </aop:aspect>
    </aop:config>

Fourth, around advice Detailed

It is a spring can provide the framework for our enhanced manual control mode when code is executed in code

ProceedingJoinPoint parameters of this type of spring to provide enhanced control execution code

/**
     * spring aop的环绕通知,手动控制增强代码的执行时机
     * @param proceedingJoinPoint
     * @return
     */
    public Object around(ProceedingJoinPoint proceedingJoinPoint){
        Object returnValue=null;
        try {
            //开启事务
            startTransaction();
            Object[] args = proceedingJoinPoint.getArgs();
            returnValue = proceedingJoinPoint.proceed(args);
            //提交事务
            commit();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            //回滚事务
            rollBack();
        } finally {
            //最终通知
        }
        return returnValue;
    }

The method of the above aop facets disposed in surrounding content as a notification.

V. Summary

During the program can be run on existing methods enhanced by aop spring, and its interior is achieved through dynamic proxy.

The key point is to use the extracted public code into the corresponding notification, the notification types: Pre-notification, the notification rear, abnormality notification, the final notification. There are a few around advice is equivalent to the former type of integration, you can manually control when to execute code enhancements in the code.

Then the spring configuration file by aop: config tag to aop configuration, including process configuration section configuration, the configuration entry point, configure the notification type.

Note that the above configuration, though using the comment + xml configuration, notify the bean class is annotated with configuration, but aop relevant configuration is configured in the spring configuration file.

Example Project Address
Example Project address

Guess you like

Origin www.cnblogs.com/chengxuxiaoyuan/p/12174392.html
Recommended