AOP---面向切面

作者: 常威爆打来福 | 来源:发表于2017-09-16 22:10 被阅读29次
一 概念

1 AOP:面向切面(方面)编程,扩展功能不需要修改源代码
2 AOP采用横向抽取机制,取代了传统纵向继承体系重复性代码

二 原理
原理
  • 第一种情况
第一种情况
  • 第二种情况
第二种情况
三 操作术语
  • *Pointcut(切入点):在类中可以增强很多方法,比如有4个方法可以被增强,可是只增强了其中两个方法,那这两个方法就叫切入点
  • *Advice(通知/增强):增强的逻辑,比如扩展日志功能,这个功能称为增强
    • 前置通知:在方法之前执行
    • 后置通知:在方法之后执行
    • 异常通知:方法出现异常
    • 最终通知:在后置之后执行
    • 环绕通知:在方法之前和之后执行
  • *Aspect(切面):把增强应用到具体方法上,过程称为切面。把增强用应用到切入点的过程。
  • Joinpoint(连接点):类里面能被增强的方法
  • Introduction(引介):向类中动态添加属性
  • Target(目标对象):增强方法所在类
  • Weaving(织入):把增强用到类的过程
  • Proxy(代理):代理类
四 AOP-AspectJ准备

1 在spring里面进行aop操作,使用AspectJ实现

  • AspectJ不是spring一部分,和spring一起使用进行aop操作
  • Spring2.0以后增加了对AspectJ支持

2 使用AspectJ实现aop有两种方式
(1)基于AspectJ的xml配置
(2)基于AspectJ的注解方式
3 jar包

jar包
  • aopalliance
  • aspectjweaver
  • aop
  • aspects

maven配置

<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>

http://repo.spring.io/release/org/springframework/spring
4 创建spring核心配置文件,导入AOP操作
5 使用表达式配置切入点
(1)切入点,实际增强的方法
(2)常用的表达式
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)

  • execution(* AOP.Book.add(..)) 增强AOP包下Book类中的add方法
  • execution(* AOP.Book.*(..)) 增强AOP包下Book类中所有方法
  • execution(* .(..)) 增强所有包下所有类中所有方法
  • 匹配所有save开头的方法execution(* save*(..))
五 基于AspectJ的xml配置

1 Book.java

package AOP;

/**
 * Created by pc on 2017/9/16.
 */
public class Book {
    public void add(){
        System.out.println("add.......");
    }
}

2 MyBook.java

package AOP;

/**
 * Created by pc on 2017/9/16.
 */
public class MyBook {
    public void before(){
        System.out.println("前置增强.........");
    }
}

3 .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"> <!-- bean definitions here -->

        <bean id="book" class="AOP.Book" />
        <bean id="mybook" class="AOP.MyBook" />
    <aop:config>
        <!--1.配置切入点-->
        <aop:pointcut id="pointcut1" expression="execution(* AOP.Book.*(..))" />
       <!-- 2.配置切面
       把增强用到方法上面-->
        <aop:aspect ref="mybook">
            <!--配置增强类型
            method:增强类里面使用哪个方法作为前置
            -->
            <aop:before method="before" pointcut-ref="pointcut1" />
        </aop:aspect>
    </aop:config>
</beans>

4 测试

        @Test
    public void test4(){
            ApplicationContext context = new ClassPathXmlApplicationContext("Spring/bean.xml");
           Book book = (Book) context.getBean("book");
           book.add();
        }
测试结果
六 基于AspectJ的注解方式

1 Books.java

package AOP;

import org.springframework.stereotype.Component;

/**
 * Created by yang on 17-10-22.
 */
//类似bean
@Component(value = "books")
public class Books {
    public void add(){
        System.out.println("add........");
    }
}

2 Mybooks.java

package AOP;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * Created by yang on 17-10-22.
 */
@Component(value = "myBooks")
//切点
@Aspect
public class MyBooks {
    @Before(value = "execution(* AOP.Books.*(..))")
    public void mybooks(){
        System.out.println("before.........");
    }
}

3 .xml配置

    <!--开启aop扫描-->
    <aop:aspectj-autoproxy/>
    <!--开启注解扫描-->
    <context:component-scan base-package="AOP"></context:component-scan>

4 测试

        @Test
    public void test5(){
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
         Books books = (Books) context.getBean("books");
         books.add();
        }
执行结果

相关文章

网友评论

    本文标题:AOP---面向切面

    本文链接:https://www.haomeiwen.com/subject/xboysxtx.html