Spring之AOP

Aspect Oriented Programming

术语

  1. 通知( Advice )切面必须要完成的工作. 五种类型通知,Before,After,After-returning,After-throwing,Around
  2. 连接点( Joinpoint )在应用执行过程中能够插入切面的一个点. 这个点可以是调用方法时, 抛出异常时, 甚至修改一个字段时.
  3. 切点( Pointcut )定义了“何处”, 通常使用明确的类和方法名称来指定切点, 或是利用正则表达式定义匹配的类和方法名称模式来指定切点.
  4. 切面( Aspect )通知和切点的结合. 通知和切点共同定义了切面的全部内容—它是什么, 在何时和何处完成其功能.
  5. 引入( Introduction )允许我们向现有的类添加新方法或属性.
  6. 织入( Weaving )将切面应用到目标对象来创建新的代理对象的过程.

demo

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lee</groupId>
    <artifactId>spring-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-test</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>

        <!-- 必须加入该jar包, 否则报错 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.9</version>
        </dependency>
    </dependencies>
</project>

切面

package com.lee.spring_test.aop;

//@Aspect
public class Audience {

    /* @Pointcut("execution(* com.lee.spring_test.service.Performer.perform(..))") private void pointCutMethod() { } */

    //声明前置通知 
    //@Before("pointCutMethod()")
    public void takeSeats() {
        System.out.println("The audience is taking their seats.");
    }

    //声明前置通知 
    //@Before("pointCutMethod()")
    public void turnOffCellPhones() {
        System.out.println("The audience is turning off their cellphones");
    }

    //声明返回通知 
    //@AfterReturning(pointcut = "pointCutMethod()", returning = "result") 
    public void applaud() {
        System.out.println("CLAP CLAP CLAP CLAP CLAP");
    }

    //声明例外通知 
    //@AfterThrowing(pointcut = "pointCutMethod()", throwing = "ex") 
    public void demandRefund() {
        System.out.println("Boo! We want our money back!");
    }
}

接口

package com.lee.spring_test.service;

public interface Performer {

    void perform();
}

实现类

package com.lee.spring_test.service.impl;

import com.lee.spring_test.service.Performer;

public class Player implements Performer {

    @Override
    public void perform() {

        System.out.println("Player.perform() ~~~");
    }

}

applicationContext.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-4.2.xsd">

    <!-- 使用注解方法 <aop:aspectj-autoproxy /> -->
    <bean id="audience" class="com.lee.spring_test.aop.Audience" />
    <aop:config>
        <aop:aspect ref="audience">
            <aop:pointcut id="performance" expression="execution(* com.lee.spring_test.service.Performer.perform(..))" />

            <aop:before method="takeSeats" pointcut-ref="performance" />
            <aop:before method="turnOffCellPhones" pointcut-ref="performance" />
            <aop:after-returning method="applaud" pointcut-ref="performance" />
            <aop:after-throwing method="demandRefund" throwing="ex" pointcut-ref="performance" />
        </aop:aspect>
    </aop:config>

    <bean id="kenny" class="com.lee.spring_test.service.impl.Player" />
</beans>

测试类

package com.lee.spring_test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lee.spring_test.service.Performer;

public class Test {

    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Performer performer = (Performer) ac.getBean("kenny");
        performer.perform();
    }
}

结果
Spring之AOP_第1张图片

你可能感兴趣的:(spring,AOP)