For the latest stable version, please use Spring Framework 6.2.6! |
Enabling @AspectJ Support
To use @AspectJ aspects in a Spring configuration, you need to enable Spring support for configuring Spring AOP based on @AspectJ aspects and auto-proxying beans based on whether or not they are advised by those aspects. By auto-proxying, we mean that, if Spring determines that a bean is advised by one or more aspects, it automatically generates a proxy for that bean to intercept method invocations and ensures that advice is run as needed.
The @AspectJ support can be enabled with XML- or Java-style configuration. In either
case, you also need to ensure that AspectJ’s aspectjweaver.jar
library is on the
classpath of your application (version 1.9 or later). This library is available in the
lib
directory of an AspectJ distribution or from the Maven Central repository.
Enabling @AspectJ Support with Java Configuration
To enable @AspectJ support with Java @Configuration
, add the @EnableAspectJAutoProxy
annotation, as the following example shows:
-
Java
-
Kotlin
@Configuration
@EnableAspectJAutoProxy
public class AppConfig {
}
@Configuration
@EnableAspectJAutoProxy
class AppConfig
使用 XML 配置启用 @AspectJ 支持
要使用基于 XML 的配置启用 @AspectJ 支持,请使用aop:aspectj-autoproxy
元素,如下例所示:
<aop:aspectj-autoproxy/>
这假定您使用架构支持,如 基于 XML 架构的配置中所述。
请参阅 AOP 架构以了解如何
将aop
Namespace。