9. How to Create a Controller Class with @Controller and Constructor Injection in Spring Boot

The following demonstrates how to create a controller package

The following shows how to create a controller class inside the controller package.

The BookController.java file contains the following implementation.

BookController.java

package com.codersstash.book_details.controller;

import com.codersstash.book_details.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class BookController {
    @Autowired
    private BookService bookService;

    public BookController(BookService bookService) {
        this.bookService = bookService;
    }

}

You can find the related GitHub repository branch here : book-details-controller

Leave a Reply

Your email address will not be published. Required fields are marked *