启用 @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>
© . This site is unofficial and not affiliated with VMware.