【问题标题】:No qualifying bean of type available in SpringBoot ApplicationSpring Boot 应用程序中没有可用类型的限定 bean
【发布时间】:2020-01-10 07:31:32
【问题描述】:

运行我的 SpringBoot 应用程序时,我收到此错误:

运行时发生异常。 null:InvocationTargetException:创建名为“bookController”的bean时出错:通过字段“bookRepository”表达的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“com.example.experimental001.Repositories.BookRepository”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)} -> [帮助1]

我已经阅读了很多关于这个错误的帖子,但似乎没有一个能解决我的问题。

我已经尝试添加 @Controller 注释,但它似乎并没有解决 bean 问题。

控制器:

@RestController
@RequestMapping("/api/books")
public class BookController {

    @Autowired
    private BookRepository bookRepository;

    @GetMapping
    public Iterable findAll() {
        return bookRepository.findAll();
    }

    @GetMapping("/title/{bookTitle}")
    public List findByTitle(@PathVariable String bookTitle) {
        return bookRepository.findByTitle(bookTitle);
    }

    @GetMapping("/{id}")
    public Book findOne(@PathVariable Long id) {
        return bookRepository.findById(id)
                .orElseThrow(() -> new BookNotFoundException("Book not found", null));
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public Book create(@RequestBody Book book) {
        return bookRepository.save(book);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {
        bookRepository.findById(id).orElseThrow(() -> new BookNotFoundException("Book not found", null));
        bookRepository.deleteById(id);
    }

    @PutMapping("/{id}")
    public Book updateBook(@RequestBody Book book, @PathVariable Long id) {
        if (book.getId() != id) {
            throw new BookIdMismatchException("error");
        }
        bookRepository.findById(id).
                orElseThrow(() -> new BookNotFoundException("Book not found", null));
        return bookRepository.save(book);
    }
}

简单控制器:

@Controller
public class SimpleController {
    @Value("${spring.application.name}")
    String appName;
    @GetMapping("/")
    public String homePage(Model model) {
        model.addAttribute("appName", appName);
        return "home";
    }
}

存储库:

@EnableJpaRepositories
@Repository

public interface BookRepository extends CrudRepository<Book, Long> {
    public List<Book> findByTitle(String title);
}

波姆:

<properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

申请文件:

@SpringBootApplication
@ComponentScan(basePackages = BookRepository)

public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

【问题讨论】:

    标签: spring spring-boot


    【解决方案1】:

    您可能需要将以下内容添加到主应用程序类中:

    @ComponentScan(basePackages = "仓库所在的包")

    看起来您的存储库位于不同的打包程序中,并且 spring 上下文在启动时无法获取它。

    请参考此文档:https://www.baeldung.com/spring-component-scanningfor

    【讨论】:

    • 应该将此注释添加到我的图书存储库类中吗?
    • 不应该将其添加到您拥有@SpringBootApplication 的邮件应用程序类中。此外,一旦您使用组件扫描,您必须指定要扫描的其他组件
    • 我将此添加到我的主应用程序类中:@ComponentScan(basePackages = com.example.experimental001.BookRepository)。但我收到此错误:experimental001/src/main/java/com/example/experimental001/Application.java:[13,42] 找不到符号 [ERROR] 符号:class Experiment001 [ERROR] location: package com.example
    • 你可以尝试只使用包名吗?
    • 我修复了这个问题:@ComponentScan(basePackages = {"BookController","BookRepository"})
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-04
    • 2020-07-05
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 2015-03-27
    相关资源
    最近更新 更多