我正在尝试直接在 Spring Boot 的控制器类中将 commandlinerunner 接口实现为 lambda 表达式,作为其功能接口。这本来应该作为第一件事运行,但它没有。如果我创建一个单独的类并添加注释,这会很有效@Component。
.
.
import org.springframework.boot.CommandLineRunner;
@RestController
@RequestMapping("/api/v1")
public class BookController {
@Autowired
private BookRepository bookRepository;
public BookController(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
CommandLineRunner obj = (String... args) -> {
Book entity1 = new Book("How to stay focused", "Miriyam Bali");
Book entity2 = new Book("Turn the World", "Cliyo Mathew");
Book entity3 = new Book("New Heights", "Arsana Jyesh");
Book entity4 = new Book("Create into leaves", "Nicholas A Buzaz");
List<Book> books = Arrays.asList(entity1, entity2, entity3, entity4);
this.bookRepository.saveAll(books);
};
潇湘沐
相关分类