fieldbook需要一个名为'book'的bean,但找不到该bean

fzsnzjdm  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(325)

当服务器无法运行时,我在spring boot中遇到了这个问题
应用程序启动失败
说明:
com.example.bookproject.servies.bookservices中的fieldbook需要一个名为'book'的bean,但找不到该bean。
注入点具有以下注解:-@org.springframework.beans.factory.annotation.autowired(required=true)
行动:
考虑在配置中定义一个名为'book'的bean。
我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.BookProject</groupId>
    <artifactId>BookProject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Book</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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-devtools</artifactId>

        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.4.1</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.4.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.2.3.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.22</version>
        </dependency>

    </dependencies>

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

</project>

以及我的实体类

package com.example.BookProject.Model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="book")
public class Book {
    @Id
    private String id;

    @Column(unique = true,nullable = false ,name = "BookTitle")
    private String title;

    @Column(name = "BookDecription",unique = false, nullable = true)
    private String Decription;

    public Book() {
        super();
    }

    public Book(String id, String title, String decription) {
        super();
        this.id = id;
        this.title = title;
        Decription = decription;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDecription() {
        return Decription;
    }

    public void setDecription(String decription) {
        Decription = decription;
    }

}

我的故事:

package com.example.BookProject.Repostory;

    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.stereotype.Repository;

    import com.example.BookProject.Model.Book;
    @Repository
    public interface BookRepo extends JpaRepository<Book, String> {

}

我的服务:

package com.example.BookProject.Servies;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.BookProject.Model.Book;
import com.example.BookProject.Repostory.BookRepo;

@Service
@Transactional
public class BookServies {
    //i am inject BookRepo Here
    @Autowired
    public BookRepo book;

    //find all Book
    public List<Book> FindAllBook(){
        return book.findAll();
    }

    //find ById Book

    public Book FindById(String id)
    {
        return book.findById(id).get();
    }

    //Add new Book
    public void AddBook(Book newBook) {
        book.save(newBook);

    }

    public void DeleteBook(String id) {
        book.deleteById(id);
    }

}

我的控制器:

package com.example.BookProject.Controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.BookProject.Model.Book;
import com.example.BookProject.Servies.BookServies;

@RestController
@RequestMapping(value = "api/book")
public class BookController {
    //inject servies
    @Autowired
    private BookServies book;

    //find all book
    @GetMapping(value = "/allBook")
    public List<Book> AllBook(){
        return book.FindAllBook();
    }

    //Add New Book Method
    @PostMapping(value = "/addBook")
    public void AddBook(@RequestBody Book NewBook)
    {
        book.AddBook(NewBook);

    }

    //Find Book By Id
    @GetMapping(value = "bookId/{id}")
    public Book FindBook(@PathVariable String id)
    {
        return book.FindById(id);
    }

    //DeleteBook By Id
    @GetMapping(value = "/deleteId/{id}")
    public void DeleteBook(@PathVariable String id)
    {
        book.DeleteBook(id);
    }

}
qrjkbowd

qrjkbowd1#

这个问题是由于依赖冲突,spring boot starter数据jpa依赖就足够了,因为包括hibernate core 5.x.x最终版本。

从上图我希望你能理解这个问题,
解决方案:
删除了不需要的依赖项并解决了冲突

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</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-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

</dependencies>

删除不需要的依赖项后,应用程序启动,
[2m2021-01-09 02:47:53.923[0;39m[32m信息[0;39m[35m14703[0;39m[2m---[0;39m[2m[干管][0;39m[36mo.s.b.w.embedded.tomcat.tomcatwebserver[0;39m[2m:[0;39m tomcat已在端口8080(http)上启动,上下文路径为“”[2m2021-01-09 02:47:53.926[0;39m[32m信息[0;39m[35m14703[0;39m[2m---[0;39m[2m[干管][0;39m[36mcom.knf.dev.mockito.demoapplication[0;39m[2m:[0;39m在1.927秒内启动demoapplication(jvm运行时间为2.316)

相关问题