You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot实体基于AOP扩展元数据方案失效问题求助

问题:Spring Boot AOP无法拦截接口默认方法getMetadata

我有一个Spring Boot 2.7.6应用,部分实体由Mongo仓库管理,想要为实体扩展元数据。计划构建元数据仓库存储所有实体的元数据,让实体实现带默认方法的IMetadata接口,通过切面从元数据仓库加载数据并返回。


代码结构

IMetadata接口

package io.zzzz.interfaces;
//imports are there

public interface IMetadata {
  @JsonIgnore
  default List<Metadata> getMetadata() {
    return null;
  }
}

SampleEntity实体类

package io.zzzz.entities;
//imports are there

@Getter
@Setter
@NoArgsConstructor
@Configurable
@Document("samples")
public class SampleEntity implements IMetadata {
  @Id private String id;
  // ... many other fields
}

MetadataAspect切面类

package io.zzzz;
//imports are there

@Aspect
@Component
@EnableAspectJAutoProxy(proxyTargetClass=true)
@Slf4j
public class MetadataAspect implements ApplicationContextAware {
  @Override
  public void setApplicationContext(ApplicationContext context) throws BeansException {
    metadataService = context.getBean(MetadataService.class);
  }

  @Around("execution(* getMetadata(..))")
  public Object getMetadataAdvice(ProceedingJoinPoint joinPoint) {
    log.debug("Accessing metadata ...");
    var metadataOwner = joinPoint.getTarget();
    var md = metadataService.getMetadataForEntity((IMetadata) metadataOwner);
    log.debug("Accessed metadata.");
    return md;
  }
}

已添加的依赖

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-instrument</artifactId>
  <version>5.3.24</version>
  <scope>runtime</scope>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.9.7</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.9.7</version>
</dependency>

配置操作

  • 已在应用中添加@EnableLoadTimeWeaving注解
  • 配置VM参数:-javaagent:<path>/spring-instrument-5.3.24.jar
  • 因方案未生效,在META-INF下创建aop.xml:
<aspectj>
  <weaver>
    <include within="io.zzzz.entities..*"/>
  </weaver>
  <aspects>
    <aspect name="io.zzzz.MetadataAspect"/>
  </aspects>
</aspectj>

当前问题

添加VM参数-Daj.weaving.verbose=true后能看到织入操作,通用切面可以拦截服务方法,但getMetadata方法始终未被拦截,不知道如何解决,怀疑该方案不可行。

内容的提问来源于stack exchange,提问作者Andreas Rieger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 04:10:21