这是从这个问题开始的:
Spring autowired bean for @Aspect aspect is null
我最初的理解是,在使用 Spring AOP 时,使用 @Aspect 注释的类被创建为 Spring 托管 bean,因此依赖注入(inject)将正常工作。但是,带有@Aspect 注释的对象似乎是作为spring 容器外部的单例创建的,因此我必须像这样在XML 中配置它才能将其作为spring 托管bean 启用:
<bean id="aspect" class="com.mysite.aspect" factory-method="aspectOf" />
现在这完全让我感到困惑。我以为下面的配置会使用spring AOP:
<context:component-scan base-package="com.mysite.aspectPackage"/>
<aop:aspectj-autoproxy/>
所以它会使用组件扫描创建方面 bean 来扫描 @Aspect 注释,然后 autoproxy 会创建一个 beanPostProcessor,它使用适当的建议代理我的上下文中的所有 bean。然后我想启用 aspectJ 我需要一个完全不同的 XML 配置(顺便说一下,我似乎无法在文档中找到一个示例)。正是这种配置使用 aspectJ 来创建我的容器之外的方面,或者通过操作字节码而不是代理来工作。
注意
这不是关于spring AOP和aspect J区别的问题,这里说得很清楚:
最佳答案
@Aspect
不是spring注解,component-scan检测不到。因此,您必须以某种方式将其注册为 spring bean。 aspectOf
解决方案有效。你也可以试试
@Aspect
@Component
关于java - 启用 Spring AOP 或 AspectJ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9666922/