无法为类的参数[IProductManager]插入值,不存在类型的Bean

问题描述

Micronaut的依赖项注入问题。我正在使用Micronaut 2.1.0版,并不断遇到依赖项注入问题。

{
  "message": "Internal Server Error: Failed to inject value for parameter [IProductManager] of class: fete.bird.api.v1.controller.ProductController\n\nMessage: No bean of type [fete.bird.manager.IProductManager] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).\nPath Taken: new ProductController([IProductManager IProductManager])"
}

接口

@Introspected
public interface IProductManager {
    List<ProductViewModel> findFreeText(String text);
}

实施

@Singleton
public class ProductManager implements IProductManager{
    private final ApplicationContext applicationContext;
    private static final Logger LOG = LoggerFactory.getLogger(ProductManager.class);
    public ProductManager(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public List<ProductViewModel> findFreeText(String text) {
        LOG.info("Manager --> Finding all the products");
        final List<ProductViewModel> model = new ArrayList<>();
        
         return model;
    }
}

控制器

@Controller("/api/v1/product")
public class ProductController {
    private static final Logger LOG = LoggerFactory.getLogger(ProductController.class);
    private final IProductManager iProductManager;

    public ProductController(IProductManager IProductManager) {
        this.iProductManager = IProductManager;
    }

    @Get(uri = "/{text}")
    List<ProductViewModel> freeTextSearch(String text) {
        LOG.info("Controller --> Finding all the products");
        return iProductManager.findFreeText(text);
    }
}

我正在使用Intellj IDE。如果删除构建文件夹并运行应用程序,则一切正常,但是运行该应用程序时,多次仍会出现上述错误。每次我需要删除构建文件夹以使其正常工作

解决方法

我希望您遇到https://github.com/micronaut-projects/micronaut-core/issues/4277中描述的错误。

在这种情况下,您可以按照该错误报告中的说明,通过禁用增量编译来解决问题。请注意,执行完全干净的构建也可以解决该问题,但只能等到下一次该构建触发相同的问题时。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...