Spring AOP:Xml vs AspectJ方法

谢恩

因此,我的问题主要围绕将Spring AOP与基于XML的架构结合使用,而不是与AspectJ结合使用。通过在线浏览,我一直在尝试找出采用AOP的方法。一种情况使我有些困惑。

假设我有许多具有n个方法的类,并且我想将我的方面类的建议应用于某些方法/连接点,但不是全部,我可以看到在使用AspectJ时这将非常简单-我只是应用我的方面注释应使用建议的方法。但是,从我所看到的基于xml的方法的角度来看,我必须为这些方法中的每一个创建一个切入点(假设它们不能被一个表达式覆盖,即每个方法都有一个不同的名称)和(如果我是使用基于代理的方法)每个目标/类的代理类。从这个意义上讲,AspectJ方法看起来更整洁。

那么,我对这两种方法的理解是正确的,还是我错过了可以实现xml方法更整洁解决方案的Spring AOP的某些部分?

很抱歉,冗长的解释,但我想让情况尽可能清楚...

用户名

听起来您正在尝试在Spring AOP和AspectJ之间做出选择,但是您假设Spring AOP需要基于XML的配置。没有。您可以对Spring AOP和AspectJ都使用AspectJ批注:

package com.example.app;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;

@Aspect
public class NotificationAspect {
    @Autowired private NotificationGateway notificationGateway;

    @Pointcut("execution(* com.example.app.ItemDeleter.delete(com.example.app.Item))")
    private void deleteItemOps() { }

    @AfterReturning(pointcut = "deleteItemOps() && args(item)")
    public void notifyDelete(Item item) {
        notificationGateway.notify(item, ConfigManagementEvent.OP_DELETE);
    }
}

因此,如果您尝试比较Spring AOP和AspectJ,则将AspectJ与基于注释的Spring AOP进行比较比较明智。

Spring AOP通常更简单(您不需要AspectJ编译器);因此,除非您需要更多特殊的切入点,否则参考文档建议使用Spring AOP而不是AspectJ。

更新:响应以下OP的评论,我们可以使用XML配置来建议特定的方法:

<aop:config>
    <aop:pointcut
        id="deleteItemOps"
        expression="execution(* com.example.app.ItemDeleter.delete(com.example.app.Item))" />
    <aop:advisor
        advice-ref="notificationAdvice"
        pointcut-ref="deleteItemOps() && args(item)" />
</aop:config>

如果您想将切入点直接嵌入中<aop:advisor>,也可以这样做:

<aop:config>
    <aop:advisor
        advice-ref="notificationAdvice"
        pointcut="execution(* com.example.app.ItemDeleter.delete(com.example.app.Item)) && args(item)" />
</aop:config>

(我还没有检查&& args(item)XML配置一部分,但是我认为我给出的示例是可以的。如果它不起作用,请尝试将其删除并随意相应地编辑答案。)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用Spring AOP配置XML的Aspectj

来自分类Dev

在 Spring AOP 或 AspectJ 中拦截带注释的类和方法

来自分类Dev

通过Spring AOP了解AspectJ样式

来自分类Dev

使用AspectJ的Spring AOP:加载时间编织

来自分类Dev

通过Spring AOP + Aspectj进行异常处理

来自分类Dev

没有Spring AOP的Aspectj入门

来自分类Dev

Spring AOP / AspectJ记录方法的执行时间,但是如何将参数传递给它?(Spring Boot API)

来自分类Dev

Spring AOP使用AspectJ起作用还是什么?

来自分类Dev

使用AspectJ CTW时禁用Spring AOP MethodSecurityInterceptor

来自分类Dev

方法参数aspectj不工作spring boot

来自分类Dev

注释Spring AOP AspectJ不能与JSF2视图中使用的Managed Bean中的方法一起使用吗?

来自分类Dev

AspectJ没有看到META-INF / aop.xml

来自分类Dev

如何使用Spring AOP记录方法链接

来自分类Dev

带有groovy的Spring AOP:调用方法

来自分类Dev

如何使用Spring AOP记录方法链接

来自分类Dev

拆分方法时未应用Spring AOP

来自分类Dev

通知Spring AOP中的方法错误

来自分类Dev

使用Spring AOP拦截Jersey资源方法

来自分类Dev

未调用方法后的Spring Aop

来自分类Dev

AspectJ在Spring @Controller方法上不起作用

来自分类Dev

带有Kotlin和Gradle的Spring AOP(AspectJ)-我无法正常工作

来自分类Dev

spring什么时候使用不带Aspectj.jar的AOP?

来自分类Dev

Spring Aspectj的含义

来自分类Dev

Spring AspectJ,在方法执行之前的切入点,其中注释了方法OR类

来自分类Dev

使用spring aop在jar文件中剖析方法未发生

来自分类Dev

Spring AOP:使用后返回方法获取返回类型

来自分类Dev

控制器方法的Spring AOP传递参数

来自分类Dev

Spring AOP-确定方法是否由@Scheduled调用

来自分类Dev

Spring AOP不是使用@Around注释的劫持方法

Related 相关文章

  1. 1

    使用Spring AOP配置XML的Aspectj

  2. 2

    在 Spring AOP 或 AspectJ 中拦截带注释的类和方法

  3. 3

    通过Spring AOP了解AspectJ样式

  4. 4

    使用AspectJ的Spring AOP:加载时间编织

  5. 5

    通过Spring AOP + Aspectj进行异常处理

  6. 6

    没有Spring AOP的Aspectj入门

  7. 7

    Spring AOP / AspectJ记录方法的执行时间,但是如何将参数传递给它?(Spring Boot API)

  8. 8

    Spring AOP使用AspectJ起作用还是什么?

  9. 9

    使用AspectJ CTW时禁用Spring AOP MethodSecurityInterceptor

  10. 10

    方法参数aspectj不工作spring boot

  11. 11

    注释Spring AOP AspectJ不能与JSF2视图中使用的Managed Bean中的方法一起使用吗?

  12. 12

    AspectJ没有看到META-INF / aop.xml

  13. 13

    如何使用Spring AOP记录方法链接

  14. 14

    带有groovy的Spring AOP:调用方法

  15. 15

    如何使用Spring AOP记录方法链接

  16. 16

    拆分方法时未应用Spring AOP

  17. 17

    通知Spring AOP中的方法错误

  18. 18

    使用Spring AOP拦截Jersey资源方法

  19. 19

    未调用方法后的Spring Aop

  20. 20

    AspectJ在Spring @Controller方法上不起作用

  21. 21

    带有Kotlin和Gradle的Spring AOP(AspectJ)-我无法正常工作

  22. 22

    spring什么时候使用不带Aspectj.jar的AOP?

  23. 23

    Spring Aspectj的含义

  24. 24

    Spring AspectJ,在方法执行之前的切入点,其中注释了方法OR类

  25. 25

    使用spring aop在jar文件中剖析方法未发生

  26. 26

    Spring AOP:使用后返回方法获取返回类型

  27. 27

    控制器方法的Spring AOP传递参数

  28. 28

    Spring AOP-确定方法是否由@Scheduled调用

  29. 29

    Spring AOP不是使用@Around注释的劫持方法

热门标签

归档