关于使用Aspect的NoSuchBeanDefinitionException

时间:2016-05-20 07:44:51

标签: spring spring-aop aspect

使用的Spring启动版本: 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;
}

事情进展顺利。当我在下面添加用于拦截空中呼叫呼叫响应时间的代码时出现问题:

@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

1 个答案:

答案 0 :(得分:0)

从日志中说:

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();