zoukankan      html  css  js  c++  java
  • Spring学习(20)--- Schema-based AOP(基于配置的AOP实现) -- 配置切入点pointcut

    pointcut(切断点)表达式:

    • execution(public * *(..))  
    • execution(* set*(..))  
    • execution(* com.xyz.service.AccountService.*(..))  
    • execution(* com.xyz.service..(..))  
    • execution(* com.xyz.service...(..))  
    • within(com.xyz.service.*) (only in Spring AOP)
    • within(com.xyz.service..*) (only in Spring AOP)
    • this(com.xyz.service.AccountService) (only in Spring AOP)
    • ....

         execution用于匹配方法执行的连接点

    举几个例子:

    • execution(public * *(..))       切入点为执行所有public方法时 
    • execution(* set*(..))        切入点为执行所有set开始的方法时
    • execution(* com.xyz.service.AccountService.*(..))        切入点为执行AccountService类中所有方法时
    • execution(* com.xyz.service..(..))            切入点为执行com.xyz.service包下的所有方法时
    • execution(* com.xyz.service...(..))            切入点为执行com.xyz.service包及其子包下的所有方法时
    • within(com.xyz.service.*) (only in Spring AOP)
    • within(com.xyz.service..*) (only in Spring AOP)            within 用于匹配制定类型内的执行方法
    • this(com.xyz.service.AccountService) (only in Spring AOP)      this 用于匹配当前AOP代理对象类型的执行方法

     其他

    • target(com.xyz.service.AccountService)  (only in Spring AOP)    target 用于匹配当前目标对象类型的执行方法
    • args(java.io.Serializable)   (only in Spring AOP)        args 用于匹配当前执行的方法传入的参数为指定类型的执行方法

    基于注解的匹配

    • @target(org.springframework.transaction.annotation.Transactional)  (only in Spring AOP)
    • @within(org.springframework.transaction.annotation.Transactional)  (only in Spring AOP)
    • @annotation(org.springframework.transaction.annotation.Transactional)  (only in Spring AOP)
    • @args(com.xyz.security.Classified)   (only in Spring AOP)

    实现:

    <aop:config>
              <aop:pointcut id="businessService" expression="execution(* com.aop.schema..(..))">
              
              </aop:pointcut>
    </aop:config>
    

    例子:

    新建两个类

    package com.aop.schema;
    /**
     * 
     * 切面类
     *
     */
    public class MyAspect {
    
    }
    
    package com.aop.schema;
    
    /**
     * 
     * 业务类
     *
     */
    public class ApsectBiz {
    
    }
    

    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"
            xmlns:context="http://www.springframework.org/schema/context"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-4.1.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
             
         <bean id="myAspect" class="com.aop.schema.MyAspect"></bean>
          
         <bean id="apsectBiz" class="com.aop.schema.ApsectBiz"></bean>
          
         <aop:config>
              <aop:aspect id="myAspectAOP" ref="myAspect">
                <aop:pointcut id="myPointcut" expression="execution(* com.aop.schema.ApsectBiz.*(..))" />
              </aop:aspect>
         </aop:config>
     
    </beans>
    
  • 相关阅读:
    SQL 函数:Aggregate、Scalar函数介绍
    SQL 用于各种数据库的数据类型:MySQL、SQLsever
    SQL 通用数据类型解析
    SQL NULL 函数:使用方法及案例剖析
    SpringBoot的配置文件
    SpringBoot简介及快速入门
    本地仓库和远程仓库分支回退到指定的历史版本(idea)
    maven配置阿里云镜像仓库
    SSM三大框架整合-分模块版本
    Java程序调用Oracle存储过程和存储函数
  • 原文地址:https://www.cnblogs.com/JsonShare/p/4633564.html
Copyright © 2011-2022 走看看