Spring AOP - 排除特定方面?

发布于 2024-10-06 20:51:59 字数 199 浏览 2 评论 0原文

我正在使用 Spring 3.0.5,并且想知道是否有可能以某种方式排除加载已用 @Aspect 构造型注释的方面类,但同时包含其他方面注释的类?如果你要走注释路线(我就是这样),这似乎是全有或全无,我已经尝试过查看但似乎找不到任何暗示这一点的东西。

原因是我有一个包含方面的中央核心库,但我可能不想在使用该中央库创建的每个项目中包含这些方面。

谢谢。

I'm using Spring 3.0.5, and was wondering if it's possible, to somehow exclude aspect classes from being loaded that have been annotated with the @Aspect stereotype, but at the same time, include other aspect annotated classes? It seems to be an all or nothing if you're going the annotation route(which I am) I've tried looking at the and but can;t seem to find anything that hints at this.

The reason for this is that I have a central core library which contains aspects, but I may not want to include these aspects in every project I create using this central library.

Thanks.

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

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

发布评论

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

评论(1

憧憬巴黎街头的黎明 2024-10-13 20:51:59

回答这个问题需要很长时间...

如果您将 AspectJ 注释与 Spring AOP 一起使用,而不是 AspectJ 运行时或编译时编织,那么您很幸运。 Spring 只会选择 @Aspect 注解的类(如果这些类也使用 @Component 之类的注解)。 Spring 不认为 @Aspect 是组件扫描的候选者。如果您使用 XML 配置,只需从配置中删除该 bean 即可。

我建议不要使用会影响核心库的组件扫描。例如,如果您的应用程序是 com.example.myapp1 并且您的核心库是 com.example.corelibrary.*,请确保您的组件扫描仅查看 com.example.myapp1 而不是 com.example。

由于这个确切的原因,在应用程序的基础包之外进行组件扫描是安全的。使用 bean 的 XML 配置引入各个方面。

This is a long time for an answer...

If you are using AspectJ annotations with Spring AOP and not AspectJ runtime or compile-time weaving then you're in luck. Spring will only pick up @Aspect annotated classes if they're annotated with something like @Component as well. Spring does not consider @Aspect a candidate for component scanning. If you're using XML configuration, simply remove that bean from your config.

I would suggest NOT using component scanning that would hit a core library. For example, if your app is com.example.myapp1 and your core library is com.example.corelibrary.* make sure your component scanning is looking at com.example.myapp1 ONLY and not com.example.

It is not safe to component scan outside of your app's base package because of this exact reason. Pull in the individual aspects with an XML config for the bean.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文