java 如何在类上创建一个方面,这不是使用 Spring AOP 的 bean?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2470014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:20:41  来源:igfitidea点击:

How to create an aspect on class, that is not a bean using Spring AOP?

javaspringaopaspectj

提问by Ula Krukar

I work on an legacy application, where Spring AOP (namely ProxyFactoryBean) is used.

我在使用 Spring AOP(即ProxyFactoryBean)的遗留应用程序工作。

I need to add an aspect around a method of a certain class. This class is nota bean however. The AspecjJ pointcut expression would be like this: execution(* xyz.package.Class.method())

我需要围绕某个类的方法添加一个方面。然而,这个类不是一个 bean。AspecjJ 切入点表达式如下所示: execution(* xyz.package.Class.method())

I created a MethodInterceptorand AspectJExpressionPointcut, but I don't know how make those two work together.

我创建了一个MethodInterceptorand AspectJExpressionPointcut,但我不知道如何让这两个一起工作。

EDIT:
I do not have source code for this class, it is a 3rd party library. The instances of this class are not created by me, neither in source code, nor in spring configuration as beans. It is used internally by the library.

编辑
我没有这个类的源代码,它是一个 3rd 方库。这个类的实例不是我创建的,既不是在源代码中,也不是在spring配置中作为bean。它由图书馆内部使用。

Any help appreciated.

任何帮助表示赞赏。

采纳答案by axtavt

You can use load-time weaving with full AspectJ support as described here, it doesn't require access to the source of classes being advised nor control over their instantiation (though it requires <context:load-time-weaver />and presence of the weaver itself using -javaagent:...or other methods).

您可以使用加载时编织用完整的AspectJ的支持,说明在这里,它并不需要访问的类的源被告知,也没有控制自己的实例化(尽管它需要<context:load-time-weaver />和自身使用韦弗的存在-javaagent:...其他方法)。

回答by Bozho

Try @Configurable. It is explained in this docs.

试试@Configurable。在此文档对此进行了解释。

The @Configurable annotation marks a class as eligible for Spring-driven configuration

@Configurable 注解将一个类标记为适合 Spring 驱动的配置

(you'd need <context:load-time-weaver />)

(你需要<context:load-time-weaver />

UpdateYou can make a 3rd party component a bean by listing it in applicationContext.xmlas <bean class=".." />(you don't need @Configurablewith that)

更新您可以通过上市使其第三方组件豆applicationContext.xml<bean class=".." />(你不需要@Configurable与)