(4)Spring框架AOP

学习spring中的AOP要明确的事
a、开发阶段(我们做的) 编写核心业务代码(开发主线):大部分程序员来做,要求熟悉业务需求。 把公用代码抽取出来,制作成通知(日志、事务、权限)。开发阶段最后再做。编写日志、事务、权限的配置,在spring配置文件中,声明切入点与通知间的关系,即切面即可。
b、运行阶段(Spring框架完成的) Spring框架监控切入点方法的执行。一旦监控到切入点方法被运行,使用代理机制,动态创建目标对象的代理对象,根据通知类别,在代理对象的对应位置,将通知对应的功能织入,完成完整的代码逻辑运行。

AOP (Aspect Oriented Programing) 称为:面向切面编程,它是一种编程思想。AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码的编写方式(应用场景:例如性能监视、事务管理、安全检查、缓存、日志记录等)

在这里插入图片描述

AOP底层实现代理机制。
spring的aop的底层原理
1:JDK代理(要求目标对象面向接口)(spring默认的代理方式是JDK代理)
2:CGLIB代理(面向接口、面向类)

AOP相关术语

Joinpoint(连接点): (方法)所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点。
Pointcut(切入点): (方法)所谓切入点是指我们要对哪些Joinpoint进行拦截的定义。
Advice(通知/增强): (方法)所谓通知是指拦截到Joinpoint之后所要做的事情就是通知。 通知的类型:前置通知,后置通知,异常通知,最终通知,环绕通知。 Target(目标对象): 代理的目标对象。

Weaving(织入): (了解)是指把增强应用到目标对象来创建新的代理对象的过程。 spring采用动态代理织入,而AspectJ采用编译期织入和类装载期织入。
Proxy(代理): 一个类被AOP织入增强后,就产生一个结果代理类。 Aspect(切面): (类)是切入点和通知(引介)的结合。
Introduction(引介): (不用了解)引介是一种特殊的通知在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field。

配置AOP

【需求】在访问业务层com.service.impl包中的AccountService.java中的方法之前,添加日志操作。
第1步:导入maven坐标

<?xml version="1.0" encoding="UTF-8"?>
<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.itheima</groupId>
    <artifactId>springaop</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>
    </dependencies>
</project>

第2步:定义Service的接口和实现类创建包com.service,创建接口AccountService.java

/**
 * 账户的业务层接口
 */
public interface AccountService {

    /**
     * 模拟保存账户
     */
    void saveAccount();

    /**
     * 模拟更新账户
     * @param i
     */
    void updateAccount(int i);

    /**
     * 删除账户
     * @return
     */
    int  deleteAccount();
}

创建包com.service.impl,创建接口的实现类AccountServiceImpl.java

/**
 * 账户的业务层实现类
 */
public class AccountServiceImpl implements AccountService {

    public void saveAccount() {
        System.out.println("保存信息中!!!");
    }

    public void deleteAccount(int i) {
        System.out.println("删除信息中!!!"+i);

    }

    public int updateAccount() {
        System.out.println("更新信息中!!!");
        return 0;
    }
}

第3步:创建com.util,创建增强类Logger.java
【需求】:在方法新增、修改、删除方法之前,先生成一个日志(打印一句话)

/**
 * 用于记录日志的工具类,它里面提供了公共的代码
 */
public class Logger {
//手动配置环绕通知
public Object aroundPrintLog(ProceedingJoinPoint joinPoint){
    Object proceed=null;
    try {
        System.out.println("Around【前置通知】开始记录日志...");
        proceed = joinPoint.proceed(joinPoint.getArgs());
        System.out.println("Around【后置通知】开始记录日志...");
    } catch (Throwable throwable) {
        throwable.printStackTrace();
        System.out.println("Around" +
                "【异常通知】法开始记录日志了。。。");
    }finally {
        System.out.println("Around【最终通知】开始记录日志...");
    }
    return proceed;
}}

第4步:配置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.xsd">
  <!--logger类,声明切面(创建对象,不是真正aop的切面) -->
    <bean id="logger" class="com.itheima.utils.Logger"></bean>
    <!--配置AOP-->
    <aop:config>
        <!--配置切面 -->
        <aop:aspect id="logAdvice" ref="logger">
            <!-- 配置通知的类型,并且建立通知方法和切入点方法的关联--><aop:before method="printLog" pointcut="execution(void   com.itheima.service.impl.AccountServiceImpl.saveAccount())"></aop:before>
            <aop:before method="printLog" pointcut="execution(void com.itheima.service.impl.AccountServiceImpl.updateAccount(int))"></aop:before>
            <aop:before method="printLog" pointcut="execution(int com.itheima.service.impl.AccountServiceImpl.deleteAccount())"></aop:before>
        </aop:aspect>
    </aop:config>
</beans>

spring中基于XML的AOP配置步骤
1、把通知Bean也交给spring来管理
2、使用aop:config标签表明开始AOP的配置
3、使用aop:aspect标签表明配置切面
id属性:是给切面提供一个唯一标识
ref属性:是指定通知类bean的Id。
4、在aop:aspect标签的内部使用对应标签来配置通知的类型
我们现在示例是让printLog方法在切入点方法执行之前执行:所以是前置通知
aop:before:表示配置前置通知
method属性:用于指定Logger类中哪个方法是前置通知
pointcut属性:用于指定切入点表达式,该表达式的含义指的是对业务层中哪些方法增强

切入点表达式的写法:
关键字:execution(表达式)

第5步:测试创建com.itheim.test包,创建测试类AOPTest.java

/**
 * 测试AOP的配置
 */
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class AOPTest {

    @Autowired
    private AccountService as;

    @Test
    public void proxy(){
        //3.执行方法
        as.saveAccount();
        as.deleteAccount(1);
        as.updateAccount();
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值