啟用 @AspectJ 支援
要在 Spring 配置中使用 @AspectJ 切面,你需要啟用 Spring 支援,以便基於 @AspectJ 切面來配置 Spring AOP,並根據 Bean 是否被這些切面增強來自動代理 Bean。透過自動代理,我們指的是如果 Spring 確定一個 Bean 被一個或多個切面增強,它會自動為該 Bean 生成一個代理,以攔截方法呼叫並確保按需執行通知。
@AspectJ 支援可以透過程式設計式或 XML 配置啟用。在任何一種情況下,你都需要確保 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>