当前位置: 首页 > 知识库问答 >
问题:

在REST API中创建bean名称时发生UnsatifiedPendencyException错误

巫马俊力
2023-03-14

**2021-11-26 20:30:57.375警告11700---[restartedMain]ConfigServletWebServerApplicationContext:上下文初始化期间遇到异常-取消刷新尝试:org。springframework。豆。工厂UnsatisfiedPendencyException:创建名为“bookRestController”的bean时出错:未满足的依赖项通过字段“bookService”表示;嵌套的异常是org。springframework。豆。工厂UnsatisfiedPendencyException:创建名为“bookServiceImpl”的bean时出错:通过字段“bookService”表示的未满足的依赖关系;嵌套的异常是org。springframework。豆。工厂BeanCurrentlyIncremationException:创建名为“bookServiceImpl”的bean时出错:请求的bean当前正在创建中:是否存在无法解析的循环引用?2021-11-26 20:30:57.376信息11700---[restartedMain]j.LocalContainerEntityManagerFactoryBean:关闭持久化单元“默认”的JPA EntityManagerFactory 2021-11-26 20:30:57.382信息11700---[restartedMain]com。扎克瑟。希卡里。HikariDataSource:HikariPool-1-已启动关机。。。2021-11-26 20:30:57.393信息11700---[restartedMain]com。扎克瑟。希卡里。HikariDataSource:HikariPool-1-关闭完成。2021-11-26 20:30:57.396信息11700---[restartedMain]o.apache。卡塔琳娜。果心标准服务:停止服务[Tomcat]2021-11-26 20:30:57.410信息11700---[restartedMain]条件评估报告日志监听器:

启动应用程序上下文时出错。要显示条件报告,请在启用调试的情况下重新运行应用程序。2021-11-26 20:30:57.437ERROR 11700 --- [ restartedMain]o. s. b. d. LoggingFailureAnalysis:**

应用程序无法启动

描述:

应用程序上下文中某些bean的依赖关系形成一个循环:

bookRestController(字段private BookAuthorManyToManyRelationship.service.BookService BookAuthorManyToManyRelationship.Rest.bookRestController.BookService)┌─────┐ | bookServiceImpl(字段private booksauthormanytomanyrelationship.service.BookService booksauthormanytomanyrelationship.serviceImpl.bookServiceImpl.BookService)└─────┘

行动:

不鼓励依赖循环引用,默认情况下禁止它们。更新应用程序以删除bean之间的依赖循环。作为最后的手段,可以通过将spring.main.allow循环引用设置为true来自动打破循环。*

基本项目应用。JAVA

```package BookAuthorManyToManyRelationship;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class BasicProjectApplication {

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

}```

**AuthorDAO.java**
```package BookAuthorManyToManyRelationship.dao;

import java.util.List;

import BookAuthorManyToManyRelationship.entity.Author;
import BookAuthorManyToManyRelationship.entity.Book;

public interface AuthorDAO {
    public Author findById(int id);
    public List<Book> findListOfBookWrittenByAuthor();
    public void deleteById(int id);
    public void save(Author author);
}```

**BookDAO.java**

```package BookAuthorManyToManyRelationship.dao;

import java.util.List;

import BookAuthorManyToManyRelationship.entity.Author;
import BookAuthorManyToManyRelationship.entity.Book;

public interface BookDAO {
    public Book findById(int id);
    public List<Author> findListOfAuthorWhoHasWrittenThisBook();
    public void deleteById(int id);
    public void save(Book book);
}```

**AuthorDAOImpl.java**

```package BookAuthorManyToManyRelationship.daoImpl;

import java.util.List;

import javax.persistence.EntityManager;

import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import BookAuthorManyToManyRelationship.dao.AuthorDAO;
import BookAuthorManyToManyRelationship.entity.Author;
import BookAuthorManyToManyRelationship.entity.Book;
@Repository
public class AuthorDAOImpl implements AuthorDAO {
    @Autowired
    private EntityManager entityManager;

    @Override
    public Author findById(int id) {
        Session session = entityManager.unwrap(Session.class);
        Author author = session.get(Author.class, id);
        
        return author;
    }

    @Override
    public List<Book> findListOfBookWrittenByAuthor() {
        
        Session session = entityManager.unwrap(Session.class);
        Query<Book> theQuery = session.createQuery("from Book",Book.class);
        
        List<Book> book = theQuery.getResultList();
        return book;
    }

    @Override
    public void deleteById(int id) {
        
        Session session = entityManager.unwrap(Session.class);
        
        Query theQuery = session.createQuery("delete from Author where author_id=:theid");
        theQuery.setParameter("theid", id);
        theQuery.executeUpdate();
    }

    @Override
    public void save(Author author) {
        Session session = entityManager.unwrap(Session.class);
        session.saveOrUpdate(author);
    }

}```
**BookDAOImpl.java**

```package BookAuthorManyToManyRelationship.daoImpl;

import java.util.List;

import javax.persistence.EntityManager;

import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import BookAuthorManyToManyRelationship.dao.BookDAO;
import BookAuthorManyToManyRelationship.entity.Author;
import BookAuthorManyToManyRelationship.entity.Book;
@Repository
public class BookDAOImpl implements BookDAO {
    
    @Autowired
    private  EntityManager  entityManager;

    @Override
    public Book findById(int id) {
        
        Session session = entityManager.unwrap(Session.class);
        Book b = session.get(Book.class, id);
        return b;
    }

    @Override
    public List<Author> findListOfAuthorWhoHasWrittenThisBook() {
        
        Session session = entityManager.unwrap(Session.class);
        Query<Author> q = session.createQuery("from Author",Author.class);
        List<Author> author = q.getResultList();
        return author;
    }

    @Override
    public void deleteById(int id) {
        
        Session session = entityManager.unwrap(Session.class);
        Query q = session.createQuery("delete from Book where book_id:=theid");
        q.setParameter("theid", id);
        q.executeUpdate();
    }

    @Override
    public void save(Book book) {
        
        Session session = entityManager.unwrap(Session.class);
        session.saveOrUpdate(book);
        
    }

}```

**Address.java**

```package BookAuthorManyToManyRelationship.entity;

import javax.persistence.Embeddable;

@Embeddable
public class Address {
    String street;
    String city;
    String country;
    public Address(String street, String city, String country) {
        super();
        this.street = street;
        this.city = city;
        this.country = country;
    }
    public Address() {
        super();
        // TODO Auto-generated constructor stub
    }
    public String getStreet() {
        return street;
    }
    public void setStreet(String street) {
        this.street = street;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    
}```
**Author.java**

```package BookAuthorManyToManyRelationship.entity;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="author")
public class Author {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    int id;
    String authorName;
    Address address;
    @ManyToMany(cascade = CascadeType.ALL)
    List<Book> book;
    public Author() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Author(int id, String authorName,Address address,List<Book> book) {
        super();
        this.id = id;
        this.authorName = authorName;
        this.address = address;
        this.book = book;
    }
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getAuthorName() {
        return authorName;
    }
    public void setAuthorName(String authorName) {
        this.authorName = authorName;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    public List<Book> getBook() {
        return book;
    }
    public void setBook(List<Book> book) {
        this.book = book;
    }
    
}```
**Book.java**

```package BookAuthorManyToManyRelationship.entity;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="book")
public class Book {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    int id;
    String book_name;
    String subject;
    @ManyToMany(cascade = CascadeType.ALL)
    List<Author> author;
    public Book() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Book(int id, String book_name, String subject, List<Author> author) {
        super();
        this.id = id;
        this.book_name = book_name;
        this.subject = subject;
        this.author = author;
    }
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getBook_name() {
        return book_name;
    }
    public void setBook_name(String book_name) {
        this.book_name = book_name;
    }
    public String getSubject() {
        return subject;
    }
    public void setSubject(String subject) {
        this.subject = subject;
    }
    public List<Author> getAuthor() {
        return author;
    }
    public void setAuthor(List<Author> author) {
        this.author = author;
    }
    
    
}```

**AuthorRestController**

```package BookAuthorManyToManyRelationship.Rest;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
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 BookAuthorManyToManyRelationship.entity.Author;
import BookAuthorManyToManyRelationship.entity.Book;
import BookAuthorManyToManyRelationship.service.AuthorService;

@RestController
@RequestMapping("/welcome")
public class AuthorRestController {
    
    @Autowired
    private AuthorService authorService;
    

    @GetMapping("/author/{id}")
    public Author getAuthorById(@PathVariable int id )
    {
        return authorService.findById(id);
    }
    
    
    @GetMapping("/book")
    public List<Book> getBook()
    {
        return authorService.findListOfBookWrittenByAuthor();
    }
    
    @PostMapping("/saveAuthor")
    public void setAuthor(@RequestBody Author author)
    {
        authorService.save(author);
    }
    
    
}```

**BookRestController.java**

```package BookAuthorManyToManyRelationship.Rest;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
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 BookAuthorManyToManyRelationship.entity.Author;
import BookAuthorManyToManyRelationship.entity.Book;
import BookAuthorManyToManyRelationship.service.BookService;

@RestController
@RequestMapping("/abc")
public class BookRestController {
    
    @Autowired
    private BookService bookService;
    
    
    @GetMapping("/book/{id}")
    public Book getBookById(@PathVariable int id )
    {
        return bookService.findById(id);
    }
    
    @GetMapping("/author")
    public List<Author> getAuthor()
    {
        return bookService.findListOfAuthorWhoHasWrittenThisBook();
    }
    
    
    @PostMapping("/saveBook")
    public void setBook(@RequestBody Book book)
    {       
        bookService.save(book);
    }
    
}```

**AuthorService.java**

```package BookAuthorManyToManyRelationship.service;

import java.util.List;


import BookAuthorManyToManyRelationship.entity.Author;
import BookAuthorManyToManyRelationship.entity.Book;

public interface AuthorService {
    public Author findById(int id);
    public List<Book> findListOfBookWrittenByAuthor();
    public void deleteById(int id);
    public void save(Author author);
}```

**BookService.java**

```package BookAuthorManyToManyRelationship.service;

import java.util.List;

import BookAuthorManyToManyRelationship.entity.Author;
import BookAuthorManyToManyRelationship.entity.Book;
public interface BookService {

    public Book findById(int id);
    public List<Author> findListOfAuthorWhoHasWrittenThisBook();
    public void deleteById(int id);
    public void save(Book book);
    
}```

**AuthorServiceImpl.java**

```package BookAuthorManyToManyRelationship.serviceImpl;

import java.util.List;

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

import BookAuthorManyToManyRelationship.dao.AuthorDAO;
import BookAuthorManyToManyRelationship.entity.Author;
import BookAuthorManyToManyRelationship.entity.Book;
import BookAuthorManyToManyRelationship.service.AuthorService;
@Service
public class AuthorServiceImpl implements AuthorService {

    @Autowired
    private AuthorDAO authorDAO;
    
    
    @Override
    @Transactional
    public Author findById(int id) {
        return authorDAO.findById(id);
    }

    @Override
    @Transactional
    public List<Book> findListOfBookWrittenByAuthor() {
        return authorDAO.findListOfBookWrittenByAuthor();
    }

    @Override
    @Transactional
    public void deleteById(int id) {
        authorDAO.deleteById(id);
    }

    @Override
    @Transactional
    public void save(Author author) {
        authorDAO.save(author);
    }

}```

**BookServiceImpl.java**

```package BookAuthorManyToManyRelationship.serviceImpl;

import java.util.List;


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

import BookAuthorManyToManyRelationship.entity.Author;
import BookAuthorManyToManyRelationship.entity.Book;
import BookAuthorManyToManyRelationship.service.BookService;
@Service
public class BookServiceImpl implements BookService {
    
    @Autowired
    private BookService bookService;
    

    @Override
    @Transactional
    public Book findById(int id) {
        return bookService.findById(id);
    }

    @Override
    @Transactional
    public List<Author> findListOfAuthorWhoHasWrittenThisBook() {
        return bookService.findListOfAuthorWhoHasWrittenThisBook();
    }

    @Override
    @Transactional
    public void deleteById(int id) {
        bookService.deleteById(id);
    }

    @Override
    @Transactional
    public void save(Book book) {
        bookService.save(book);
    }
    
}
```

共有1个答案

华峰
2023-03-14

这就是你问题的原因

@Service
public class BookServiceImpl implements BookService {
    
@Autowired
private BookService bookService

我不明白为什么在它自己的实现中有BookService的参考,我猜你想在这里添加booksdao

 类似资料:
  • 我有一个数据库配置类来连接我的Spring网络服务和数据库。我正在使用Spring引导,使它成为独立的应用程序。 这是我的课 每次我尝试运行我的代码,它都会抛出异常: 据我所知,有一个缺失的依赖项,但我不知道是哪个。或者问题是别的什么?这是我在pom.xml的依赖项 你知道问题的原因和解决方法吗?

  • 我正在尝试测试我的应用程序,我已经尝试解决它三天了,我寻找stackoverflow,但仍然无法解决它。我的问题是,Autowired始终为空,即使我将所有建议导入为空 或者 始终为null并给出此错误

  • 问题内容: 我正在将SpringMVC用于Web应用程序以及为Hibernate注入sessionFactory。 运行我的应用程序时,出现以下错误… 我不确定为什么autoInject无法正常工作。我已将所有XML配置放入servlet上下文中… userManagementController的代码是… userService是userService.java … 由…使用 谁能给我任何指示?

  • 我得到以下错误消息,而运行我的项目 组织。springframework。豆。工厂UnsatisfiedPendencyException:创建名为“TipoeStatDoCivilController”的bean时出错:通过字段“TipoeStatDoCivilService”表示的未满足的依赖关系;嵌套的异常是org。springframework。豆。工厂UnsatisfiedPendenc

  • 问题内容: 我将所有的XML Spring配置都转换为Java代码配置,但是由于我有一个丑陋的异常,所以我无法运行我的所有测试(它们之前都曾进行过测试): 这是我的测试课: 和我的: 我在测试课上尝试了推子,但这没用。该项目是Spring MVC项目,但是现在我仅测试服务层,所以为什么要在我的课程上放置该注释?即使有了该注释,也会出现另一个错误。那么,这里发生了什么? 问题答案: 您的配置很好,除

  • 问题内容: 几天来,我一直在尝试创建Spring CRUD应用程序。我糊涂了。我无法解决此错误。 还有这个 客户端控制器 ClientServiceImpl 客户资料库 我浏览了许多类似的问题,但是没有人回答不能帮助我。 问题答案: ClientRepository应该用标记注释。使用你当前的配置,Spring将不会扫描该类并对其有所了解。在启动和连接时,找不到ClientRepository类。