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

Spring Converter的convert方法无法触发@LogExecTime注解切面求助

Hey there! Let's break down why your @LogExecTime annotation isn't working on the Converter's convert() method, and walk through practical fixes for it.

Why the Aspect Isn't Triggered

Spring AOP relies on dynamic proxies (either JDK proxies for interfaces or CGLIB for classes). When you inject a standard Spring bean, you're actually getting a proxy wrapper that handles weaving in aspect logic.

The problem here is that when ConversionService calls your SampleConverter's convert() method, it's invoking the original object directly—not the proxy. That skips the aspect's advice entirely, so your timing logs never run.

Solutions to Make the Aspect Work

1. Switch to AspectJ Weaving (Most Robust Fix)

AspectJ weaves aspect logic directly into the target class's bytecode, not just into a proxy. This means no matter how the method is called—whether through a proxy or directly—the aspect will execute.

Set Up Load-Time Weaving (LTW)
  • Add AspectJ dependencies to your project (Maven example):
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.9.21</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.21</version>
</dependency>
  • Enable LTW in your Spring config:
@Configuration
@EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.ENABLED)
public class AspectConfig {
    // Keep your existing LogTimeAspect bean here
}
  • Start your JVM with the AspectJ weaver agent:
-javaagent:/path/to/aspectjweaver-1.9.21.jar

Once set up, your @LogExecTime annotation will work for all method calls, including those from ConversionService.

2. Manually Wrap the Converter in a Proxy

If you don't want to use AspectJ, you can explicitly create a proxy for your Converter and register that proxy with the ConversionService.

Example Code
@Configuration
public class ConverterConfig {

    @Autowired
    private LogTimeAspect logTimeAspect;

    @Bean
    public Converter<SourceType, ResultType> sampleConverter() {
        SampleConverter originalConverter = new SampleConverter();
        // Create a proxy that applies the LogTimeAspect
        ProxyFactory proxyFactory = new ProxyFactory(originalConverter);
        proxyFactory.addAdvice(logTimeAspect);
        return (Converter<SourceType, ResultType>) proxyFactory.getProxy();
    }

    @Bean
    public ConversionService conversionService(List<Converter<?, ?>> converters) {
        DefaultConversionService conversionService = new DefaultConversionService();
        converters.forEach(conversionService::addConverter);
        return conversionService;
    }
}

Now when ConversionService calls convert(), it's hitting the proxy, so your aspect will trigger.

3. Ensure ConversionService Uses the Spring-Managed Proxy

If your SampleConverter is already a Spring bean (marked with @Component), the issue might be that ConversionService is grabbing the original object instead of the proxy. Fix this by explicitly injecting the bean into your ConversionService setup:

Update Your Converter and ConversionService
@Component
public class SampleConverter implements Converter<SourceType, ResultType> {
    @Override
    @LogExecTime
    public ResultType convert(SourceType source) {
        // Your conversion logic here
    }
}

@Bean
public ConversionService conversionService(SampleConverter sampleConverter) {
    DefaultConversionService conversionService = new DefaultConversionService();
    conversionService.addConverter(sampleConverter);
    return conversionService;
}

This guarantees the ConversionService uses the proxy-wrapped bean, not the original class instance.

Quick Recap

  • The root issue is Spring AOP's proxy-based approach only works when methods are called through the proxy.
  • AspectJ weaving is the most reliable fix (works for all method invocation scenarios).
  • Manual proxy wrapping or explicit bean injection are simpler alternatives if you don't want to adopt AspectJ.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:16:52