使用 Aspect 时出现 NoSuchBeanDefinitionException

Posted

技术标签:

【中文标题】使用 Aspect 时出现 NoSuchBeanDefinitionException【英文标题】:NoSuchBeanDefinitionException on using Aspect 【发布时间】:2016-05-20 07:44:50 【问题描述】:

使用的 Spring Boot 版本: 1.2.3

我们正在基于如下属性创建 CacheManager:

@Bean
public CacheManager cacheManager() throws IOException 
    String cacheImpl = env.getProperty(CacheSupport.CACHE_PROPERTY, String.class, "");
    if (CacheSupport.AEROSPIKE.equalsIgnoreCase(cacheImpl)) 
        return aerospikeCacheManager();
     else if (CacheSupport.REDIS.equalsIgnoreCase(cacheImpl)) 
        return redisCacheManager();
     else 
        return guavaCacheManager();
    

我们有一个 AerospikeController,如下所示:

@ConditionalOnProperty(name = CacheSupport.CACHE_PROPERTY, havingValue = CacheSupport.AEROSPIKE)
@Controller
public class AerospikeController 
  @Autowired
    private AerospikeCacheManager aerospikeCacheManager;

一切正常。当我添加以下代码以拦截 aerospike 调用的响应时间时,问题就出现了:

@Component

@Aspect 公共类 WebServiceResponseTimeLogger

private static final Logger LOG = LoggerFactory.getLogger(WebServiceResponseTimeLogger.class);

@Autowired
private AerospikeCacheManager cacheManager;

@PostConstruct
void init() 
    LOG.info("WebServiceRequestTimeLogger initialized");


public WebServiceResponseTimeLogger() 
    super();


@Pointcut("execution(* com.snapdeal.pie.web.aerospike.config.AerospikeCacheManager.*(..))")
public void pointcuts() 


@Around("pointcuts()")
public void logExternalCalls(ProceedingJoinPoint pjp) throws Throwable 
    long timeStartInMillisecs = System.currentTimeMillis();
    pjp.proceed();
    MethodSignature signature = (MethodSignature) pjp.getSignature();
    LOG.info("Time Taken to execute :   is  ms", new Object[]  signature, pjp.getArgs()[0], (System.currentTimeMillis() - timeStartInMillisecs) );

我遇到了异常:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.snapdeal.pie.web.aerospike.config.AerospikeCacheManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: @org.springframework.beans.factory.annotation.Autowired(required=true)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 18 common frames omitted

【问题讨论】:

在 AeospikeController 中将 @Qualifier(value = "cacheManager") 添加到自动装配的 CacheManager 或将变量名重命名为 cacheManager 【参考方案1】:

从它说的日志中:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.snapdeal.pie.web.aerospike.config.AerospikeCacheManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: @org.springframework.beans.factory.annotation.Autowired(required=true)

检查是否在此代码中正确创建了 bean AerospikeCacheManager。

@Bean
public CacheManager cacheManager() throws IOException 
    String cacheImpl = env.getProperty(CacheSupport.CACHE_PROPERTY, String.class, "");
    if (CacheSupport.AEROSPIKE.equalsIgnoreCase(cacheImpl)) 
        return aerospikeCacheManager();

【讨论】:

是的,bean 已成功创建。当我删除方面代码时,一切正常。

以上是关于使用 Aspect 时出现 NoSuchBeanDefinitionException的主要内容,如果未能解决你的问题,请参考以下文章

使用 FolderBrowserDialog 时出现异常

使用串行插件时出现 Ionic Capacitor Android 错误 - 连接到外部设备时出现 Cordova_not_available

在 keras 中使用 EfficientNet 时出现 ResourceExhaustedError

使用 htdocs xampp 运行命令时出现以下错误

使用 functools.reduce 时出现属性错误

使用此命令时出现错误,我不知道为啥? [复制]