Dependency Injection : Spring boot – CommandLineRunner interface example

10 tháng 02, 2021 - 5593 lượt xem

Ví dụ về giao diện CommandLineRunner

Bài viết gốc xem tại : https://howtodoinjava.com/spring-boot/command-line-runner-interface-example/

CommandLineRunnerGiao diện của Spring boot được sử dụng để chạy khối mã chỉ một lần trong vòng đời của ứng dụng - sau khi ứng dụng được khởi tạo.

Cách sử dụng CommandLineRunner

Bạn có thể sử dụng CommandLineRunnergiao diện theo ba cách:

1) Sử dụng CommandLineRunner dưới dạng @Component

Điều này là khá dễ dàng.

@Component

public class ApplicationStartupRunner implements CommandLineRunner {

    protected final Log logger = LogFactory.getLog(getClass());

    @Override

    public void run(String... args) throws Exception {

        logger.info("ApplicationStartupRunner run method Started !!");

    }

}

2) Triển khai CommandLineRunner trong @SpringBootApplication

Điều này cũng có thể. Mã mẫu được đưa ra dưới đây:

@SpringBootApplication

public class SpringBootWebApplication extends SpringBootServletInitializer implements CommandLineRunner {
    @Override

    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

        return application.sources(SpringBootWebApplication.class);

    }
    public static void main(String[] args) throws Exception {

        SpringApplication.run(SpringBootWebApplication.class, args);

    }

    @Override

    public void run(String... args) throws Exception {

        logger.info("Application Started !!");

    }

}

 

3) Sử dụng CommandLineRunner làm Bean

Bạn có thể định nghĩa một bean trong SpringBootApplicationđó trả về lớp thực thi CommandLineRunnergiao diện.

ApplicationStartupRunner.java

public class ApplicationStartupRunner implements CommandLineRunner {

    protected final Log logger = LogFactory.getLog(getClass());

    @Override

    public void run(String... args) throws Exception {

        logger.info("Application Started !!");

    }

}

Đăng ký ApplicationStartupRunner bean

@SpringBootApplication

public class SpringBootWebApplication extends SpringBootServletInitializer {

    @Override

    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

        return application.sources(SpringBootWebApplication.class);

    }

    public static void main(String[] args) throws Exception {

        SpringApplication.run(SpringBootWebApplication.class, args);

    }

    @Bean

    public ApplicationStartupRunner schedulerRunner() {

        return new ApplicationStartupRunner();

    }

}

Sử dụng @Order nếu nhiều triển khai giao diện CommandLineRunner

Bạn có thể có nhiều triển khai CommandLineRunnergiao diện. Theo mặc định, khởi động mùa xuân để quét tất cả các run()phương thức của nó và thực thi nó. Nhưng nếu bạn muốn buộc một số thứ tự trong chúng, hãy sử dụng @Orderchú thích.

@Order(value=3)

@Component

class ApplicationStartupRunnerOne implements CommandLineRunner {

    protected final Log logger = LogFactory.getLog(getClass());

    @Override

    public void run(String... args) throws Exception {

        logger.info("ApplicationStartupRunnerOne run method Started !!");

    }

}

@Order(value=2)

@Component

class ApplicationStartupRunnerTwo implements CommandLineRunner {

    protected final Log logger = LogFactory.getLog(getClass());

    @Override

    public void run(String... args) throws Exception {

        logger.info("ApplicationStartupRunnerTwo run method Started !!");

    }

}

Xác minh các bản ghi.


2017-03-08 13:55:04 - ApplicationStartupRunnerTwo run method Started !!

2017-03-08 13:55:04 - ApplicationStartupRunnerOne run method Started !!

Tại sao sử dụng giao diện CommandLineRunner

  • Trình chạy dòng lệnh là một chức năng hữu ích để thực thi các loại mã khác nhau mà chỉ phải chạy một lần, ngay sau khi khởi động ứng dụng.
  • FYI, Spring Batch dựa vào những người chạy này để kích hoạt việc thực hiện các công việc.
  • Chúng ta có thể sử dụng phương pháp tiêm phụ thuộc vào lợi thế của mình để kết nối bất kỳ phụ thuộc nào mà chúng ta cần và theo bất kỳ cách nào chúng ta muốn - trong run()việc triển khai phương pháp.

 

 

 

Bình luận

avatar
Ngọc Nhuận 2023-07-25 05:33:59.419433 +0000 UTC

Đầu tiên mình cảm ơn những thông tin mà bài viết mang lại, tuy nhiên không rõ tác giả lấy nguồn nước ngoài ở đâu để google dịch mà nhiều chỗ dịch gây khó hiểu cho người đọc, ví dụ như "khởi động mùa xuân để quét tất cả các run() phương thức của nó và thực thi nó", "Spring Batch dựa vào những người chạy này để kích hoạt việc thực hiện các công việc.",...

Avatar
* Vui lòng trước khi bình luận.
Ảnh đại diện
  +1 Thích
+1