Spring学习(20)--- Schema-based AOP(基于配置的AOP实现) -- 配置切入点pointcut

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

  • 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(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">

     <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>
时间: 2024-08-25 17:14:53

Spring学习(20)--- Schema-based AOP(基于配置的AOP实现) -- 配置切入点pointcut的相关文章

SPRING学习(十九)--基于注解的AOP切面编程

上篇中介绍了基于XML配置的AOP切面编程,除了XML配置AOP切面编程外,还可以通过注解方式实现AOP切面编程,本篇通过一个小例子来介绍基于注解的AOP编程. 1.在spring中使用AOP变成,不止要导入spring-aop.jar,还需要导入spring-aspects.jar.aspectjweaver.jar和aopalliance.jar,但是aspectjweaver.jar被spring-aspects.jar依赖,aopalliance.jar被spring-aop.jar依赖

spring 注解 之 AOP基于@Aspect的AOP配置

Spring AOP面向切面编程,可以用来配置事务.做日志.权限验证.在用户请求时做一些处理等等.用@Aspect做一个切面,就可以直接实现. 1.首先定义一个切面类,加上@Component  @Aspect这两个注解 @Component@Aspectpublic class LogAspect { private static final Logger logger = LoggerFactory.getLogger(LogAspect.class); private static fin

spring cloud 入门系列七:基于Git存储的分布式配置中心--Spring Cloud Config

我们前面接触到的spring cloud组件都是基于Netflix的组件进行实现的,这次我们来看下spring cloud 团队自己创建的一个全新项目:Spring Cloud Config.它用来为分布式系统中的基础设施和微服务提供集中化的外部配置支持,分为服务端和客户端两个部分. 其中服务端也称为分布式配置中心,他是独立的微服务应用,用来连接配置仓库并为客户端提供获取接口(这些接口返回配置信息.加密.解密信息等): 客户端是微服务架构中的各个微服务应用或基础设施,它们通过制定的配置中心来管理

不错的Spring学习笔记(转)

Spring学习笔记(1)----简单的实例 ---------------------------------   首先需要准备Spring包,可从官方网站上下载.   下载解压后,必须的两个包是spring.jar和commons-logging.jar.此外为了便于测试加入了JUnit包.   在Myeclipse中创建Java项目.   编写一个接口类,为了简单,只加入了一个方法.   Java代码   1.package com.szy.spring.interfacebean;  

Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较

本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同. 相关引入包等Spring  AOP编程准备,请参考小编的其他博文,这里不再赘述. 案例要求: 写一个简单的实现四则运算的计算器. 加入AOP功能:日志功能:检测参数中是否有负数的功能. 废话不多说了,直接上代码: (一)基于XML配置: 定义了一个接口类: package com.edu.aop; public interface ArithmeticCalculator { int add(i

Spring学习4-面向切面(AOP)之schema配置方式

一.通过Scheme配置实现AOP步骤(Spring AOP环境的环境与上篇博文 Spring接口方式相同)    步骤一.编写业务类: public class AspectBusiness {    //切入点     public String delete(String obj) {         System.out.println("==========调用切入点:" + obj + "说:你敢删除我!===========\n");        

spring学习笔记(23)基于tx/aop配置切面增强事务

在上一篇文章中,我们使用了声明式事务来配置事务,使事务配置从service逻辑处理中解耦出来.但它还存在一些缺点: 1. 我们只针对方法名的特定进行拦截,但无法利用方法签名的其它信息定位,如修饰符.返回值.方法入参.异常类型等.如果我们需要为同名不同参的同载方法配置不同事务就会出问题了. 2. 事务属性的配置串虽然能包含较多信息,但配置较易出错. 针对这些问题,我们可以基于Schema,引入tx和aop的命名空间来改进我们的配置: 引入命名空间 <beans xmlns="http://w

Spring框架笔记(二十三)——基于配置文件的方式来配置 AOP

配置实现IOC功能时,我们采用了配置文件xml和注解两类方式实现.实现AOP功能时我们也可以使用两种方式.前面我们介绍了AOP基于注解的实现方式,本文我将采用基于配置文件的方式完成从原始对象bean.切面bean.切点及通知配置的方法. 用基于 XML 的配置声明切面 除了使用 AspectJ 注解声明切面, Spring 也支持在 Bean 配置文件中声明切面. 这种声明是通过 aop schema 中的 XML 元素完成的. 正常情况下, 基于注解的声明要优先于基于 XML 的声明. 通过

【Spring】基于@Aspect的AOP配置

Spring AOP面向切面编程,可以用来配置事务.做日志.权限验证.在用户请求时做一些处理等等.用@Aspect做一个切面,就可以直接实现. ·   本例演示一个基于@Aspect的小demo 1.新建一个Maven工程 2.引入相关maven依赖 1 <project xmlns="http://maven.apache.org/POM/4.0.0" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quo

spring aop 基于schema的aop

AOP的基本概念: 连接点(Jointpoint):表示需要在程序中插入横切关注点的扩展点,连接点可能是类初始化.方法执行.方法调用.字段调用或处理异常等等,Spring只支持方法执行连接点,在AOP中表示为"在哪里干": 切入点(Pointcut):选择一组相关连接点的模式,即可以认为连接点的集合,Spring支持perl5正则表达式和AspectJ切入点模式,Spring默认使用AspectJ语法,在AOP中表示为"在哪里干的集合":(选取我们所需要的连接点的集