啟用 @AspectJ 支援
要在 Spring 配置中使用 @AspectJ 切面,你需要啟用 Spring 對基於 @AspectJ 切面的 Spring AOP 配置的支援,並根據 bean 是否被這些切面通知來自動代理 bean。自動代理意味著,如果 Spring 確定一個 bean 被一個或多個切面通知,它會自動為該 bean 生成一個代理來攔截方法呼叫,並確保按需執行通知。
可以透過程式設計或 XML 配置來啟用 @AspectJ 支援。在任何一種情況下,你還需要確保 AspectJ 的 org.aspectj:aspectjweaver 庫位於應用程式的類路徑中(版本 1.9 或更高)。
-
Java
-
Kotlin
-
Xml
@Configuration
@EnableAspectJAutoProxy
public class ApplicationConfiguration {
}
@Configuration
@EnableAspectJAutoProxy
class ApplicationConfiguration
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<aop:aspectj-autoproxy />
</beans>