E
E
Eka22016-07-16 07:52:46
Spring
Eka2, 2016-07-16 07:52:46

How does the relationship between classes work in a Spring Boot Application?

Please explain how the relationship between the classes in the example is organized. Why are the methods written in the BookServiceImpl class, which implements the BookService interface, executed, because only the interface is connected from the BooksController, and not its implementation. What happens if there are several options for implementing the BookService interface?

@RestController
@RequestMapping(value = "/books")
public class BooksController {
  @Autowired
  private BookService bookService;
}

public interface BookService {

}

@Service
public class BookServiceImpl implements BookService {

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kosarev, 2016-07-16
@Eka2

The @Service annotation tells Spring to create an instance of this class and put it in the application context.
The @Autowired annotation indicates that the dependency should be injected into the generated instance of the class. The appropriate dependency is selected by ID, name, or class (or its parent) of the injected dependency.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question