Spring Knowledge Points (2) Aspect-Oriented AOP

AOP concept

AOP aspect Oriented Program Aspect-oriented (aspect) programming

The horizontal extraction mechanism is adopted to replace the repetitive code of the traditional vertical inheritance system. Generally, functions such as performance monitoring, transaction management, security check, and cache are realized.

principle

对于扩展功能来说
原来:修改源代码
后来:纵向抽取机制,继承BaseClass,调用super方法()
再后来:横向抽取机制

Bottom layer: dynamic proxy implementation:

使用动态代理创建接口的*实现类的代理对象*(一个对象能实现和原来的对象相同的功能,和原来的对象平级,也实现了接口,但不是真正的对象)

For objects with interfaces: jdk's dynamic
proxy For any object: cjlib dynamic proxy: create a dynamic proxy object of its subclass, and the method of calling the parent class in the subclass is enhanced
(there are three ways for the proxy to be static, jdk dynamic, cjlib dynamic)

operational terms

Joinpoint: join point: method that can be enhanced (Spring only supports join points of method type)

Pointcut: Pointcut: The method of actual enhancement

Advice: notification/enhancement: the logic of the actual enhancement method (just a method)

前置通知:
后置通知:
异常通知:出现异常时出现的通知
最终通知:在后置之后来执行的通知
环绕通知:之前之后都可以通知

Aspect: Aspect: the process of applying advice to specific methods, called aspect (the process of applying enhancements to pointcuts)

Introduction: Introduction: You can dynamically add properties and methods to classes

Target: target object: the object to which the enhancement method belongs

Weaving: Weaving: The process of applying augmentation to a target

Proxy: Proxy: After a class is enhanced by AOP, a resulting proxy class is generated

Configure using

1. Use expressions to configure pointcuts
Common expressions
execution(<access modifier>?<return type><method name>(<parameter>)<exception>)

(1)execution(* com.luhao.service.IBookService.add(..))方法要写全路径
(2)execution(* com.luhao.service.IBookService.*(..))
(3)execution(* *.*(..)) 任意类任意方法都增强
(4)save*.*(..)
(5)com.luhao.service.*(..) 不包含子包
(6) com.luhao.*..*(..) 包含子孙包
...

2. Using AspectJ to implement AOP
AspectJ is a separate aspect-oriented framework that extends the Java language. It can be used with Spring to implement AOP.
After Spring 2.0, AspectJ support has been added

There are two ways:

1.基于Aspectj的xml配置文件
2.基于Aspectj的注解的方式

process:

1.Jar包(aop,aspect,两个依赖)
2.约束(1个)
<?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"> <!-- bean definitions here -->

</beans>
3.创建类,配置对象
4.在aop-config中配置切入点
<aop:pointcut expression="execution(public *.*(..))" id="pointcut1">
切面aop:aspect
<aop:aspect ref="增强的类">
    <aop:before method="增强的类中的方法作为前置增强" pointcut-ref="pointcut1">
</aop:aspect>

aop:after-returning :后置

aop:after:final

aop:around:Around requires parameter:proceedingJoinPoint

<?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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描需要的包 -->
<context:component-scan base-package="com.luhao.*"></context:component-scan>
<!-- 创建需要的对象-->
<bean id="userAspect" class="com.luhao.aop.UserAspect"></bean>
<bean id="userService" class="com.luhao.service.impl.UserServiceImpl"></bean>
<bean id="userDao" class="com.luhao.dao.impl.UserDaoImpl"></bean>
<!-- 配置切点 -->
<aop:config>
    <aop:pointcut expression="execution(* com.luhao.*..*(..))" id="pointcut1"/>
<!-- 配置切面 -->
    <aop:aspect ref="userAspect">
<!--        <aop:before method="before" pointcut-ref="pointcut1"/>
        <aop:after-returning method="after" pointcut-ref="pointcut1"/> -->
        <aop:around method="around" pointcut-ref="pointcut1"/>
    </aop:aspect>
</aop:config>
</beans>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324504761&siteId=291194637