title: UnsatisfiedDependencyException error in creating bean name in REST API
body: <p>**
  2021-11-26 20:30:57.375  WARN 11700 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookRestController': Unsatisfied dependency expressed through field 'bookService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookServiceImpl': Unsatisfied dependency expressed through field 'bookService'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'bookServiceImpl': Requested bean is currently in creation: Is there an unresolvable circular reference?
  2021-11-26 20:30:57.376  INFO 11700 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
  2021-11-26 20:30:57.382  INFO 11700 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
  2021-11-26 20:30:57.393  INFO 11700 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
  2021-11-26 20:30:57.396  INFO 11700 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
  2021-11-26 20:30:57.410  INFO 11700 --- [  restartedMain] ConditionEvaluationReportLoggingListener :</p>
  <p>Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
  2021-11-26 20:30:57.437 ERROR 11700 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter:
  **</p>
  <hr />
  <p>APPLICATION FAILED TO START</p>
  <hr />
  <p>Description:</p>
  <p>The dependencies of some of the beans in the application context form a cycle:</p>
  <p>bookRestController (field private BookAuthorManyToManyRelationship.service.BookService BookAuthorManyToManyRelationship.Rest.BookRestController.bookService)
  ┌─────┐
  |  bookServiceImpl (field private BookAuthorManyToManyRelationship.service.BookService BookAuthorManyToManyRelationship.serviceImpl.BookServiceImpl.bookService)
  └─────┘</p>
  <p>Action:</p>
  <p>Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.*</p>
  <p><strong>BasicProjectApplication.java</strong></p>
  <pre><code>```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&lt;Book&gt; 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&lt;Author&gt; 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&lt;Book&gt; findListOfBookWrittenByAuthor() {
          
          Session session = entityManager.unwrap(Session.class);
          Query&lt;Book&gt; theQuery = session.createQuery(&quot;from Book&quot;,Book.class);
          
          List&lt;Book&gt; book = theQuery.getResultList();
          return book;
      }
  
      @Override
      public void deleteById(int id) {
          
          Session session = entityManager.unwrap(Session.class);
          
          Query theQuery = session.createQuery(&quot;delete from Author where author_id=:theid&quot;);
          theQuery.setParameter(&quot;theid&quot;, 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&lt;Author&gt; findListOfAuthorWhoHasWrittenThisBook() {
          
          Session session = entityManager.unwrap(Session.class);
          Query&lt;Author&gt; q = session.createQuery(&quot;from Author&quot;,Author.class);
          List&lt;Author&gt; author = q.getResultList();
          return author;
      }
  
      @Override
      public void deleteById(int id) {
          
          Session session = entityManager.unwrap(Session.class);
          Query q = session.createQuery(&quot;delete from Book where book_id:=theid&quot;);
          q.setParameter(&quot;theid&quot;, 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=&quot;author&quot;)
  public class Author {
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      int id;
      String authorName;
      Address address;
      @ManyToMany(cascade = CascadeType.ALL)
      List&lt;Book&gt; book;
      public Author() {
          super();
          // TODO Auto-generated constructor stub
      }
      public Author(int id, String authorName,Address address,List&lt;Book&gt; 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&lt;Book&gt; getBook() {
          return book;
      }
      public void setBook(List&lt;Book&gt; 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=&quot;book&quot;)
  public class Book {
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      int id;
      String book_name;
      String subject;
      @ManyToMany(cascade = CascadeType.ALL)
      List&lt;Author&gt; author;
      public Book() {
          super();
          // TODO Auto-generated constructor stub
      }
      public Book(int id, String book_name, String subject, List&lt;Author&gt; 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&lt;Author&gt; getAuthor() {
          return author;
      }
      public void setAuthor(List&lt;Author&gt; 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(&quot;/welcome&quot;)
  public class AuthorRestController {
      
      @Autowired
      private AuthorService authorService;
      
  
      @GetMapping(&quot;/author/{id}&quot;)
      public Author getAuthorById(@PathVariable int id )
      {
          return authorService.findById(id);
      }
      
      
      @GetMapping(&quot;/book&quot;)
      public List&lt;Book&gt; getBook()
      {
          return authorService.findListOfBookWrittenByAuthor();
      }
      
      @PostMapping(&quot;/saveAuthor&quot;)
      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(&quot;/abc&quot;)
  public class BookRestController {
      
      @Autowired
      private BookService bookService;
      
      
      @GetMapping(&quot;/book/{id}&quot;)
      public Book getBookById(@PathVariable int id )
      {
          return bookService.findById(id);
      }
      
      @GetMapping(&quot;/author&quot;)
      public List&lt;Author&gt; getAuthor()
      {
          return bookService.findListOfAuthorWhoHasWrittenThisBook();
      }
      
      
      @PostMapping(&quot;/saveBook&quot;)
      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&lt;Book&gt; 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&lt;Author&gt; 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&lt;Book&gt; 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&lt;Author&gt; findListOfAuthorWhoHasWrittenThisBook() {
          return bookService.findListOfAuthorWhoHasWrittenThisBook();
      }
  
      @Override
      @Transactional
      public void deleteById(int id) {
          bookService.deleteById(id);
      }
  
      @Override
      @Transactional
      public void save(Book book) {
          bookService.save(book);
      }
      
  }
  ```
  </code></pre>