java - 为什么我会收到 bean 'com.mypackage.service.blog.BlogService' 的 NoSuchBeanDefinitionException

标签 java spring spring-mvc spring-boot autowired

在 Spring ,当我尝试从我的 BlogController 执行操作时,我试图解决 Unresolved Bean 异常:

@Autowired BlogService blogService;
  • 我正在使用 org.springframework.stereotype.Service 服务注释。
  • 我的 ApiApplication 应用程序类使用 @ComponentScan("com.mypackage") 进行注释。
  • 服务实现是用@Service注释的,位于com.mypackage.service.blog.BlogService"
  • 该服务无法Autowired,但它是由该服务使用的@Repository,并且位于com.mypackage.repository.blog.BlogRepository code> 可以由 Controller 导入。

我的应用程序类如下所示:

package com.mypackage;

import com.mypackage.core.Core;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan({
        "com.mypackage",
        "com.mypackage.service.blog"
})
public class ApiApplication {

    private static final Logger logger = LoggerFactory.getLogger(ApiApplication.class);

    public static void main(String[] args) throws Exception {
        org.apache.ibatis.logging.LogFactory.useSlf4jLogging();
        SpringApplication.run(ApiApplication.class, args);
        logger.info("Application started!");
    }

}

这是我的com.mypackage.controller.blog.BlogController:

@RestController
@RequestMapping("/blogs")
public class BlogController {

  @Autowired
  private BlogService blogService;

  @PostMapping
  @ResponseStatus(HttpStatus.CREATED)
  Long create(@RequestBody Blog blog) {
    blogService.insert(blog);
    return blog.getId();
  }

我的com.mypackage.service.blog.BlogService类:

public interface BlogService extends CrudService<Blog, Long> {
}

我的com.mypackage.service.blog.impl.BlogServiceImpl类:

@Service
@UserManagementTx
public class BlogServiceImpl extends AbstractCrudService<BlogRepository, Blog, Long> {

    @Autowired
    public BlogServiceImpl(BlogRepository repository) {
        super(repository);
    }

}

我已经打开了调试日志,并且正在尝试找到一些提示为什么服务未导入。

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mypackage.service.blog.BlogService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

我应该将一些特定的类路径设置为 DEBUG 并将另一个设置为 INFO 吗?我在当前的调试日志中没有看到服务创建和类路径。

最佳答案

#1

@ComponentScan 在这里不需要,只需将其从应用程序的主类中删除,即 ApiApplication 即可工作。

#2

正如我们所见,BlogServiceImpl 没有实现 BlogService,这意味着 BlogService 没有具体实现,因此 Bean > 无法创建。

你需要实现BlogServiceImpl接口(interface)BlogService来告诉spring BlogServiceImplBlogService的实现

public class BlogServiceImpl implements BlogService

并且我强烈建议您遵循包结构 As per spring docs那么您将不需要包含 @ComponentScan 来创建 Bean

com
 +- example
     +- myproject
         +- Application.java
         |
         +- domain
         |   +- Customer.java
         |   +- CustomerRepository.java
         |
         +- service
         |   +- CustomerService.java
         |
         +- web
             +- CustomerController.java

关于java - 为什么我会收到 bean 'com.mypackage.service.blog.BlogService' 的 NoSuchBeanDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48199428/

相关文章:

javascript - 如何使用 ngflow Angular JS 进行单次和多次上传

java - 监控和限制 Cloud Foundry Java 应用程序的内存使用情况

java - 使用 Spring Mvc WebApplicationInitializer,但未找到 HTTP 请求的映射

java - jackson 反序列化器 - 获取模型字段注释列表

java - Spring请求线程关闭

java - Struts2 验证框架如何工作

java - Android 整数 xml 值到字符串

java - 从 textField 添加项目到 JList

java - SFTP 集成 JSCH - 无法在 session 上执行

java - 找不到依赖项 [CountryRepository] ​​: expected at least 1 bean which qualifies as autowire candidate 的合格 bean