Spring AOP中this() PCD上下文绑定与perthis()实例化模型兼容问题
Spring AOP:perthis实例化模型与this() PCD结合的错误问题
错误现象
在尝试记录切面实例与代理对象的对应关系时,使用perthis()实例化切面模型,并通过this() PCD收集代理对象上下文,触发了如下警告:
warning no match for this type name: bean [Xlint:invalidAbsoluteTypeName]
环境依赖
使用的Maven依赖配置如下:
<properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>6.0.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>6.0.3</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.19</version> </dependency> </dependencies>
切面代码
出现问题的切面实现代码:
@Aspect("perthis(com.sj.aspects.AssociationsAspect.exampleBeans())") @Component @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) public class AssociationsAspect { public AssociationsAspect(){ System.out.println("Creating aspect instance!"); } @Pointcut("execution(* com.sj.utilities.Example.*(..))") public void exampleBeans(){}; @Pointcut("execution(* com.sj.utilities.Example.*(..)) && this(bean)") public void exampleOperations(Object bean){}; @Before(value = "exampleOperations(bean)", argNames = "jp,bean") public void beforeExampleMethodsExecution(JoinPoint jp, Object bean){ System.out.println( "JoinPoint: " + jp.getStaticPart() + "\n\taspect: " + this + "\n\tobject: " + bean ); } }
尝试的调整及结果
- 将
this(bean)中的变量名bean替换为具体类型(如this(com.sj.utilities.Example)),根据文档说明这会改变绑定逻辑,但同时触发新错误:error at ::0 formal unbound in pointcut - 移除
@Aspect注解中的perthis(...)参数后,Spring容器会为每个代理对象创建独立的切面实例,但这并非预期行为——@Scope(PROTOTYPE)仅声明切面支持多实例,而非要求容器按代理对象维度创建实例,这看起来像是一个异常行为 - 最终通过
JoinPoint的反射API获取代理对象上下文可以实现需求,但对@Scope注解在有无perthis()实例化模型时的工作机制存在疑问
核心问题
- 是否存在同时使用
this()PCD收集代理对象上下文和perthis()实例化模型的有效解决方案? - 上述切面代码中的具体错误原因是什么?
内容的提问来源于stack exchange,提问作者Sergejs Jevstigņejevs
相关产品推荐
相关产品推荐

