Java:Spring AOP和重构 [英] Java: Spring AOP & Refactoring

查看:100
本文介绍了Java:Spring AOP和重构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我开始进入Spring AOP,发生了一些事情.在配置方面时,所有注解均使用Strings作为参数,而不是Class对象.

So I'm starting to get into Spring AOP, and something occurred to me. When configuring an aspect, the annotations all use Strings as parameters, rather than Class objects.

之所以提出这一点,是因为这意味着一个IDE(我使用Eclipse,所以我将在这里引用它)将无法正确重构该方面.因此,如果我有一个MyClass类,并使用方法public void foo(),则Spring AOP注释的示例将是这样:

I bring this up because this means that an IDE (I use Eclipse, so I'll just refer to it here) won't be able to refactor the aspect properly. So if I have a class MyClass, with a method public void foo(), an example of a Spring AOP annotation would be this:

@Before ("execution(public void package.MyClass.foo())")

因为注释中的参数是String,所以如果我将方法名称重构为public void foo2(),则会破坏此方面的绑定.

Because the param in the annotation is a String, if I refactored the method name to public void foo2(), it would break the binding of this aspect.

我一直在搜索各种论坛和教程,但是在我所看到的任何地方,我都只能看到String参数,这表明这是这里的唯一选择.我想您可以通过声明绑定的方式更通用一些来解决此问题:

I've been scouring various forums and tutorials, but everywhere I look I just see String parameters, which suggests that's the only option here. I guess you could work around this by being a bit more generic in how you declare the binding:

@Before ("exeuction(public void package.MyClass.set*(..))")

此新方法适用于MyClass中具有任何数量的参数(或无参数)的任何setter方法.当然,像这样含糊不清可能并非总是如此.

This new one will work for any setter method in MyClass, with any number of parameters (or no params). Of course, being vague like this might not work all the time.

无论如何,我在这里有点抱怨,已经很晚了,我有点累了,我的大脑都因为寻找答案而疲惫不堪.但是我的最后一个问题是:有没有办法设置Spring AOP注释,以便重构会影响它们?

Anyway, I'm ranting a little bit here, it's late and I'm kind of tired and my brain is all worn out from hunting for an answer to this. But my final question is this: is there any way to set up Spring AOP annotations so that refactoring will affect them?

谢谢.

推荐答案

好吧,AOP的重点是以模块化方式实现跨领域行为,并将其应用于理想情况下不了解任何方面的应用程序代码.因此,两者之间没有固定的连接.重要的是要记住,在维护和重构应用程序代码的同时,还应使用它来重构方面,以确保切入点不会中断.

Well, the whole point of AOP is to implement cross-cutting behaviour in a modular way and apply it to application code which ideally is unaware of any aspects. Thus, you have no fixed connection between the two. It is just important to remember that as much as you maintain and refactor your application code, you should also refactor your aspects with it in oder to make sure that pointcuts do not break.

话虽如此,Eclipse和其他IDE仍向您提示在安装 AJDT(AspectJ开发工具) 的地方应用了哪些方面.我主要使用完整的 AspectJ ,而不使用名为 Spring AOP 的基于代理的"AOP lite"变体,所以我不知道AJDT对Spring AOP是否有用.不过,它将与AspectJ一起通过LTW(加载时编织)应用于Spring.

Having said that, Eclipse and other IDEs still give you hints about which aspects are applied where if you install AJDT (AspectJ Development Tools). I mainly use full AspectJ, not the proxy-based "AOP lite" variant called Spring AOP, so I do not know if AJDT is any useful with Spring AOP. It will be with AspectJ applied to Spring via LTW (load-time weaving), though.

这是外观可视化"角度的屏幕截图:

以图形方式显示哪些方面适用于代码的哪些部分.如果您双击任何一条虚线,则代码编辑器将带您到应用了方面的确切位置.

It graphically shows which aspects apply to which parts of the code. If you double-click any of the dotted lines, a code editor will lead you to the exact place where an aspect is applied.

还有一个交叉引用视图,在方面代码和普通Java代码中几乎没有指示符:

There also is the cross-reference view with little indicators within aspect code as well as plain Java code:

在右侧有一个交叉引用视图,其中列出了适用于当前所选方法的所有方面.左侧没有几个图标,指示每种方法适用的建议类型.

On the right hand side there is the cross-reference view listing all aspects applying to the currently selected method. On the left hand side there are little icons indicating which types of advice apply to each method.

顺便说一句,交叉引用视图也从各个方面起作用.当您将光标放在建议中时,外部参照视图会列出建议中代码所适用的所有位置:

BTW, the cross-reference view also works from aspects. When you place the cursor to within an advice, the xref view lists all places in the code where the advice applies:

还有更多: AspectJ编译器可以通过配置方式来列出方面代码建议的所有连接点.您可以手动或什至在自动烟雾测试中检查输出是否有变化.例如,如果由于更改了某些程序包,类或方法名称而使某个建议不再适用,则会出现 Xlint警告"adviceDidNotMatch" 和/或有时"invalidAbsoluteTypeName" .此外,相关建议的交叉引用视图将为空:

There is more: The AspectJ compiler can be configured in such a way so as to list all joinpoints advised by aspect code. You can check the output for changes manually or even in an automated smoke test. For instance, if an advice does not apply anymore because you changed some package, class or method names, there will be an Xlint warning "adviceDidNotMatch" and/or sometimes also "invalidAbsoluteTypeName". Furthermore, the cross-reference view for the advice in question will be empty:

如您所见,重构应用程序或方面代码时,AJDT中有很多指示器和帮助程序.

As you have just seen, there are plenty indicators and helpers within AJDT when refactoring application or aspect code.

另一件事:如果使用AspectJ而不是Spring AOP,则可以选择使用更简洁,更具表现力的本机AspectJ语法,而不是注释样式.我绝对更喜欢前者.然后,代码编辑器中不仅包含字符串,还可以更好地突出显示语法以及代码完成和重构.

One more thing: If you use AspectJ instead of Spring AOP you have an option to use the more concise and expressive native AspectJ syntax instead of the annotation-style syntax. I definitely prefer the former to the latter. Then you have more than just strings in your code editor, better syntax highlighting and code completion and refactoring.

最后但并非最不重要的一点:您还应该为方面代码编写单元测试和集成测试,以注意方面是否损坏.手动检查很不错,但是就即时反馈而言,自动回归测试是无与伦比的. ;-)

Last, but not least: You should also write unit and maybe integration tests for your aspect code so as to notice if aspects break. Manual checks are nice, but automated regression tests are unbeatable when it comes to immediate feedback. ;-)

这篇关于Java:Spring AOP和重构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆