提问者:小点点

com中的外业手簿。实例BookProject。服务。BookServices需要一个名为“book”的bean,但找不到该bean


当运行服务器无法运行时,我遇到了Spring boot的这个问题

申请无法启动

描述:

com.example.BookProject. Servies中的字段book。BookServies需要一个名为“book”的bean,但找不到。

注入点有以下注解:-@org.springframework.beans.factory.annotation.Autow的(必需=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.Repository;
    
    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.Repository.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);
    }
    

}

共1个答案

匿名用户

问题是由于依赖冲突,spring boot starter数据jpa依赖就足够了,因为hibernate core 5。十、 包含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 INFO[0;39m[35m14703[0;39m[2m-[0;39m[2m[main][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 INFO[0;39m[35m14703[0;39m[2m---[0;39m[2m[main][0;39m[36mcom.knf.dev.mockito.DemoApplication[0;39m[2m:[0;39m在1.927秒内启动了DemoApplication(JVM运行时间为2.316)