亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

控制器中的commandlinerunner接口方法需要添加什么spring注釋

控制器中的commandlinerunner接口方法需要添加什么spring注釋

莫回無 2023-10-13 09:51:03
我正在嘗試直接在 Spring Boot 的控制器類中將 commandlinerunner 接口實現為 lambda 表達式,作為其功能接口。這本來應該作為第一件事運行,但它沒有。如果我創建一個單獨的類并添加注釋,這會很有效@Component。..import org.springframework.boot.CommandLineRunner;@RestController@RequestMapping("/api/v1")public class BookController {@Autowiredprivate 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);    };
查看完整描述

1 回答

?
瀟湘沐

TA貢獻1816條經驗 獲得超6個贊

你可以通過這個來實現這一點。它會起作用的。


.

.

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);

    };

}


查看完整回答
反對 回復 2023-10-13
  • 1 回答
  • 0 關注
  • 96 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號