问题描述
Spring-boot 有这个问题 运行时服务器无法运行
应用程序无法启动
说明:
com.example.BookProject.Servies.BookServies 中的字段 book 需要一个名为“book”的 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.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);
}
}
解决方法
问题是由于依赖冲突,spring-boot-starter-data-jpa 依赖就足够了,因为包含 hibernate-core 5.X.X Final version。
从上面的图片我希望你理解这个问题,
解决方案:
删除了不需要的依赖并解决了冲突
<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[主][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[主][0;39m] [36mcom.knf.dev.mockito.DemoApplication [0;39m [2m:[0;39m 开始] DemoApplication 1.927 秒(JVM 运行 2.316)