Spring AOP:使用它时的缺点 - 使用 Spring AOP 的 Spring 功能没有这个缺点吗?

发布于 2024-11-06 14:21:43 字数 1058 浏览 3 评论 0原文

我正在使用 Spring Framework 3.0.5 和 Spring Security 3.0.5,我对面向方面的编程有疑问。目前我正在尝试找出面向方面编程的缺点和优点。当然,我在理论上知道它们:我可以避免冗余代码,我只需要在方面进行更改,而不是在代码中到处进行等等。但我还有一些疑问:

我发现的缺点:

我使用 Spring AOP 的方面编写了一个示例应用程序。我使用注释配置了一个方面(@Pointcut、@Before、@Aspect 等)。触发切面的方法(当然是切入点的一部分)当然是不同类的一部分,并且没有任何注释。

=>我真的认为一个很大的缺点是,当观察其他类的那些方法时,不清楚它们是否触发了一个方面。它们不需要注释或其他任何东西,它们只是在方面的切入点中提到。 (我真的希望你明白我的意思)。这就是为什么我认为 AOP 也让代码更难以理解!

a) 这个问题有解决办法吗? (当我将整个配置放在 XML 文件中时,这个问题也许可以解决吗?我不这么认为。)

b) 当我使用 AspectJ 而不是 Spring AOP 时,这个问题仍然存在吗?

使用 Spring AOP 的 Spring 功能:它们没有这个缺点吗?

由于 Spring AOP 是许多 Spring 功能的一部分(就像声明式事务管理或(可能)Spring Security(?)),我仔细研究了这些功能。我根本找不到任何缺点。

c) 让我们以声明式事务管理为例:使用这些注释(@transactional)管理事务非常容易,而且我并没有真正发现上面提到的缺点。我可以看到触发特定行为的方法。 (所有@transactional方法都会触发事务行为)也许我误解了一些东西,这不是使用AOP的地方?但如果我没有误解这一点,为什么在这里可以看到哪些方法触发了方面,而为什么在上面的示例中却看不到呢?我真的很想知道这个!

谢谢您的回答! :-)

编辑:a) 和 b) 已得到解答(使用标记这些方法的 IDE),c) 仍然缺失:-)

Im working with the Spring Framework 3.0.5 and Spring Security 3.0.5 and Ive got questions to the aspect orientated programming. At the moment Im trying to figure out the disadvantages and advantages of aspect orientated programming. Of course, I know them in theory: I can avoid redundant code, I only have to make changes in the aspect, not everywhere in the code and so on. But I still got some questions:

The disadvantage I found out:

I wrote a sample application using aspects with Spring AOP. I configured an Aspect with Annotations (@Pointcut, @Before, @Aspect and so one). The methods that triggered the aspect (which were part of a Pointcut of course) were of course part of a different class and not annotated with anything.

=> I really think that one big disadvantage is that when watching those methods of the other class it was not clear that they trigger an aspect. They needed no annotations or anything else, they were just mentioned in the pointcut of the aspect. (I really hope you understand what I mean). So thats why I think that AOP makes code also less understandable!

a) Is there a solution to this problem? (Can this maybe be solved when I put the whole configuration in an XML File? I dont think so.)

b) Would this problem still exist when I would use AspectJ instead of Spring AOP?

Springs Features using Spring AOP: they dont have this disadvantage?

As Spring AOP is part of many Spring Features (just like declarative Transaction Management or (maybe) Spring Security(?)) I took a closer look at those Features. I was not able to find any disadvantage at all.

c) Lets take the declarative transaction management as an example: managing transactions is so easy with those annotations (@transactional) and I dont really find the disadvantage I mentioned above. I can see the methods that trigger specific behaviour. (all @transactional methods trigger transactional behaviour) Maybe I misunderstood something and this isnt where AOP is used? But if I did not misunderstood this, why is it here possible to see which methods trigger aspects and why isnt it possible to see in my example above? I would really like to know this!

Thank you for answering! :-)

EDIT: a) and b) are answered (use an IDE which marks those methods), c) is still missing :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌生 2024-11-13 14:21:43

对于b)点
如果您使用带有 Spring IDE 和 AspectJ 插件或 STS 的 Eclipse,IDE 将向您显示方面的编织位置。


对于 b)
如果您使用 AspectJ 和支持 AspectJ 的 IDE(带有 AspectJ 插件的 Eclipse,或 STS),那么您将在源代码中看到 Aspect 所编织的标记。

编译时 AspectJ 的一个缺点是,您无法在图书馆中编织方面。 (无需先进技术)。


对于c)点
像 @Transactional 这样的声明性方面只有一个缺点。 -- 您可能会忘记在方法上添加注释。

但是,如果您有这样的规则:类中由@Service注释的每个公共方法(或者如果您想构建自己的@TransactionalService),都是事务性的,那么您不需要在每个方法上指定@Transactional注释。 - 所以总结一下:声明性方面非常适合阅读(您不会忽视它们),并且如果您的代码非常(比方说)“个性化”(而不是术语“不一致”),那么它们就很好。但是,如果您在具有强大架构规则的环境中工作(例如 @Service 类中的每个公共方法...),那么您可以在切入点定义中写下此规则,而不是使用声明性方面。

For Point b)
If you use an Eclipse with Spring IDE and AspectJ plugin, or STS, the IDE will show you where Aspects are woven in.


For Point b)
If you use AspectJ and an IDE that supports AspectJ (Eclipse with AspectJ Plugin, or STS), then you will see markers in the souce-code where the Aspect is woven in.

An disadvantaged of Compile time AspectJ is, that you are not able to wove aspects in libraries. (without advanced techniques).


For Point c)
There is only one disadvantage of declarative Aspects like @Transactional. -- You can forget to put the annotation on the method.

But if you have for example a rule like: every public method in a Class annoteted by @Service (or if you like to build you own @TransactionalService), is transactional, then you do not need to specifiy the @Transactional annotation on each method. -- So in Summary: declarative Aspects are very good for reading (you will not overlook them), and they are good if your code is very (lets say) "individual" (instead of the term "not consistent") . But If you work in an Environment with Strong Architecural Rules (like every public method in a @Service class...), then you can Write this rules down in a Point Cut Definition, instead of using declarative Aspects.

极致的悲 2024-11-13 14:21:43

实际上,对于 a) 点,Ralph 给出的答案是相同的:将 Eclipse 与 AspectJ 插件一起使用,或者如果您无论如何都使用 Spring,请使用 STS。
这样,您将在 IDE 中看到某个方法是否与编辑器左侧的切入点匹配,以红色小箭头表示:

“在此输入图像描述”

Actually for point a) the same answer holds that Ralph gave: use Eclipse with either AspectJ plugin or if you are using Spring anyway, use STS.
That way you will see in your IDE if a certain method matches a pointcut on the left side of your editor, represented by small red arrows:

enter image description here

猫腻 2024-11-13 14:21:43

实际上,Spring AOP 支持创建自定义注释。

我使用 Advice 定义了一个名为 Loggable 绑定的注释。Loggabel 可以应用于您想要的任何方法。

public @interface Loggable {

}

@Aspect
public class EmployeeAnnotationAspect {

    @Before("@annotation(com.ben.seal.spring.aspect.Loggable)")
    public void myAdvice(){
        System.out.println("Executing myAdvice!!");
    }
}

Actually, Spring AOP supports creating custom annotations.

I defined a annotation named Loggable binding with Advice.The Loggabel could be applied to any method you want.

public @interface Loggable {

}

@Aspect
public class EmployeeAnnotationAspect {

    @Before("@annotation(com.ben.seal.spring.aspect.Loggable)")
    public void myAdvice(){
        System.out.println("Executing myAdvice!!");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文