Spring笔记--AOP

一、AOP概述

1.为什么要使用AOP

举例:当有很多service类,要求每个方法执行前打印日志,执行后打印日志...问题:输出日志的逻辑还是无法复用,每执行一次日志都需要打印日志,造成代码冗余。

2.什么是AOP

AOP全称是Aspect Oriented Programming ,即面向切面编程。

简单的说它就是把我们程序重复的代码抽取出来,在需要执行的时候,使用动态代理的技术,在不修改源码的基础上,对程序进行增强:权限校验,日志记录,性能监控,事务控制.

3.AOP相关术语

  1. 连接点(joinpoint)

    被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法。

  2. 切入点(pointcut)

    切入点是指我们要对哪些连接点进行拦截的定义

  3. 通知(advice)

    所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类

  4. 切面(aspect)

    是切入点和通知的结合

  5. 引介(introduction)

    是一种特殊的通知,在不修改代码的前提下,引介可以在运行期为类动态地添加一些方法或字段

  6. 目标对象(Target)

    要代理的目标对象(要增强的类)

  7. 织入(weave)

    将增强应用到目标的过程将advice应用到target的过程

  8. 代理(Proxy)

    一个类被AOP织入增强之后,就产生一个代理类

二、 Spring AOP的配置

pom.xml

<?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>
    <parent>
        <groupId>com.by</groupId>
        <artifactId>Spring</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>Spring_AOP_Xml</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

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

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.30</version>
        </dependency>
         <!--支持切点表达式-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
    </dependencies>
</project>

dao层

package com.by.dao;

public interface UserDao {
    void addUser();
}
package com.by.dao;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao{
    @Override
    public void addUser() {
        System.out.println("insert into ...");
    }
}

service层

package com.by.service;

public interface UserService {
    void  addUser();
}
package com.by.service;

import com.by.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService{
@Autowired
    private UserDao userDao;
    @Override
    public void addUser() {
        userDao.addUser();
       int a=6/0;
    }
}

创建增强类

package com.by.advice;

import org.aspectj.lang.ProceedingJoinPoint;
public class MyAdvice {

    public void before(){
        System.out.println("前置通知....");
    }
   public void after(){
       System.out.println("最终通知...");
   }

   public void afterReturning(){
       System.out.println("后置通知...");
   }

   public void afterThrowing(){
       System.out.println("异常通知");
   }
 public void around(ProceedingJoinPoint joinPoint){
     System.out.println("这是环绕前通知....");
    try{
     joinPoint.proceed();}
    catch (Throwable e){
        e.printStackTrace();
    }
     System.out.println("这是环绕后通知....");
 }
}

配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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/context
      			http://www.springframework.org/schema/context/spring-context.xsd
      			http://www.springframework.org/schema/aop
       			http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.by"></context:component-scan>
    <!--配置增强类-->
<bean id="myAdvice" class="com.by.advice.MyAdvice"></bean>
    <aop:config>
         <!--
           aop:pointcut:配置切点,id:切点名 expression:配置切点表达式
           execution:([修饰符] 返回值类型 包名.类名.方法名(参数))
            -->
        <aop:pointcut id="pointcut" expression="execution(* com.by.service.*.*(..))"/>

           <!--
            aop:aspect:配置切面,ref:引用bean id
            method:用于指定通知类中的增强方法名称
		    ponitcut-ref:用于指定切入点
             -->
        <aop:aspect ref="myAdvice">

            <aop:before method="before" pointcut-ref="pointcut"></aop:before>

            <aop:after method="after" pointcut-ref="pointcut"></aop:after>

            <aop:around method="around" pointcut-ref="pointcut"></aop:around>

            <aop:after-returning method="afterReturning" pointcut-ref="pointcut"></aop:after-returning>

            <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut"></aop:after-throwing>

           </aop:aspect>

    </aop:config>

</beans>

test

package com.by.web;

import com.by.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Servlet {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = ac.getBean("userServiceImpl", UserService.class);
        userService.addUser();
        System.out.println(userService.getClass());
    }
}

三、基于注解的AOP配置

常见的AOP注解

@Aspect:把当前类声明为切面类

@Before:前置通知,可以指定切入点表达式

@After:最终【finally】通知,可以指定切入点表达式

@AfterReturning:后置【try】通知

@AfterThrowing:异常【catch】通知

@Around:环绕通知

开启spring对AOP注解支持

applicationContext.xml 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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/context
      			http://www.springframework.org/schema/context/spring-context.xsd
      			http://www.springframework.org/schema/aop
       			http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.by"></context:component-scan>

    <!--自动注解aop-->
<aop:aspectj-autoproxy> </aop:aspectj-autoproxy>

</beans>

增强类中加入aop注解

package com.by.advice;

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

@Component
@Aspect
public class MyAdvice {

    @Before(value = "execution(* com.by.service.*.*(..))")
    public void before(){
        System.out.println("前置通知..................");
    }
    @After(value = "execution(* com.by.service.*.*(..))")
    public void after(){
        System.out.println("最终通知...................");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值