Spring-AOP配置及切入点表达式使用教程详解(一)

6 篇文章 0 订阅
5 篇文章 0 订阅
  • AOP(Aspect Oriented Programming):面向切面编程。

用途

  • 日志记录,性能统计,安全控制,权限管理,事务处理,异常处理,资源池管理。
AOP组成
  • Joinpoint(连接点):就是方法
  • Pointcut(切入点):就是挖掉共性功能的方法
  • Advice(通知):就是共性功能,最终以一个方法的形式呈现
  • Aspect(切面):就是共性功能与挖的位置的对应关系
  • Target (目标对象)::就是挖掉功能的方法对应的类产生的对象,这种对象是无法直接完成最终工作的
  • Weaving织入):就是将挖掉的功能回填的动态过程
  • Proxy (代理):目标对象无法直接完成工作,需要对其进行功能回填,通过创建原始对象的代理对象实现
  • Introduction(引入):就是对原始对象无中生有的添加成员变量或成员方法

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

一、入门案例

环境准备:

--------------------------service
public interface UserService {
    public void save();
}

-------------------------service实现类

public class UserServiceImpl implements UserService {

    public void save(){

        System.out.println("user service running...");
    }

}
--------------------------------------------application启动类
public class App {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) ctx.getBean("userService");
        userService.save();
    }
}


--------------------------------------------application.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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd ">

    <!--创建userService的bean-->
 <bean id="userServivce" class="com.it.service.Impl.UserServiceImpl"/>
</beans>


  • AOP开发方式
    1.XML方式(重点使用)
    2.XML+注解方式
    3.注解方式

  • 入门案例制作步骤:
    1.导入相关坐标
    2.确认要抽取的功能,并将其制作成方法保存到专用的类中,删除原始业务中对应的功能
    3.将所有进行AOP的资源加载到IOC容器中
    4.使用配置的方式描述被抽取的功能的位置,并描述被抽取的功能与对应位置的关系

  1. 运行程序

步骤一:

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>

步骤二:

//1.制作通知类,在类中定义一个方法用于完成共性功能
public class AOPAdvice {
    public void function(){
        System.out.println("AOP-共性功能");
    }
}

在这里插入图片描述
步骤三:

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

在这里插入图片描述

步骤四

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

            <!--创建userService的bean-->
            <bean id="userService" class="com.it.service.Impl.UserServiceImpl"/>

            <!--创建通知类的bean放入IOC容器-->
           <bean id="MyAdvice" class="com.it.aop.AOPAdvice"/>

                                                  <!--4.配置AOP-->
    <aop:config>
                                                  <!--5.配置切入点-->
        <aop:pointcut id="pt" expression="execution(* *..*(..))"/>
                                                    <!--6.配置切面(切入点与通知的关系)-->
        <aop:aspect ref="MyAdvice">
                                                         <!--7.配置具体的切入点对应通知中那个操作方法-->
            <aop:before method="function" pointcut-ref="pt"/>
        </aop:aspect>


    </aop:config>
           


</beans>

切入点表达式:切入点表达式是一个快速匹配方法描述的通配格式,类似于正则表达式

关键字      (访问修饰符 返回值 包名.                  类名.方法名  (参数)异常名)
execution  (public     User  com.it.service.UserService.findById(int))

           切入点表达式——关键字
execution :匹配执行指定方法(常用)
args :匹配带有指定参数类型的方法(偶尔用)
within :……
this :……
target :……
@within :……
@target :……
@args :……
@annotation :……
bean :……
reference pointcut :……

       切入点表达式——通配符
 * :单个独立的任意符号,可以独立出现,也可以作为前缀或者后缀的匹配符出现
 execution(public * com.it.*.UserService.find**))
      匹配com.it包下的任意包中的UserService类或接口中所有find开头的带有一个参数的方法开头的带有一个参数的方法

 .. :多个连续的任意符号,可以独立出现,常用于简化包名与参数的书写
 execution(public User com..UserService.findById(..))
 匹配com包下的任意包中的UserService类或接口中所有名称为findById的方法
  
+:专用于匹配子类类型
execution(* *..*Service+.*(..))
	匹配任意包下任意Service子类的所有方法


		切入点表达式——逻辑运算符
&& :连接两个切入点表达式,表示两个切入点表达式同时成立的匹配
|| :连接两个切入点表达式,表示两个切入点表达式成立任意一个的匹配
 ! :连接单个切入点表达式,表示该切入点表达式不成立的匹配

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值