简体   繁体   English

spring 注释需要在 controller 中的 commandlinerunner 接口方法上放置什么

[英]What spring annotation needs to place on commandlinerunner interface method in controller

I am trying to implement commandlinerunner interface as lambda expression directly in my controller class of spring boot as its a functional interface.我正在尝试直接在我的 controller class 的 Z2A2D59Z76ED9A0B24D06 的 controller class 中实现 commandlinerunner 接口作为 lambda 表达式作为其功能接口。 And this was suppose to run as the first thing but it didn't.这本来应该作为第一件事运行,但事实并非如此。 This works well if i create a separate class and add annotation @Component .如果我创建一个单独的 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);
    };

You could achieve that by this.你可以通过这个来实现。 it will work.它会起作用的。

.
.
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.

相关问题 Spring CommandLineRunner接口实现 - Spring CommandLineRunner Interface Implementation 如何从Spring Controller启动CommandLineRunner - How to launch CommandLineRunner from Spring Controller Spring 方面调用接口方法上的自定义注释 - Spring aspect call on custom annotation on interface method Spring Boot 中 CommandLineRunner 的确切用途是什么 - What is the exact purpose of the CommandLineRunner in Spring Boot 这个 Spring Boot controller 方法注释有什么问题? (请求的路径在 class 级别定义,路径变量在方法中定义) - What is wrong in this Spring Boot controller method annotation? (the path of the request is defined at class level and the path variable at method) 通过控制器方法上的注释的Spring AOP不起作用 - Spring aop by annotation on controller method does not work Spring Controller中的init方法(注释版) - Init method in Spring Controller (annotation version) 存储库保存的工作方式与 controller 或 spring 启动中的命令行运行程序不同 - repository save works differently from controller or commandlinerunner in spring boot 在Spring控制器方法参数中使用接口 - Use interface in Spring controller method argument Spring boot CommandLineRunner 使用 run 方法调用所有类 - Spring boot CommandLineRunner invokes all classes with run method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM