[英]What spring annotation needs to place on commandlinerunner interface method in controller
我正在尝试直接在我的 controller class 的 Z2A2D59Z76ED9A0B24D06 的 controller class 中实现 commandlinerunner 接口作为 lambda 表达式作为其功能接口。 这本来应该作为第一件事运行,但事实并非如此。 如果我创建一个单独的 class 并添加注释@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);
};
你可以通过这个来实现。 它会起作用的。
.
.
import org.springframework.boot.CommandLineRunner;
@RestController
@RequestMapping("/api/v1")
public class BookController {
@Autowired
private BookRepository bookRepository;
public BookController(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
@Bean
public CommandLineRunner run() throws Exception {
return 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);
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.