|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2019 the original author or authors. |
| 2 | + * Copyright 2012-2021 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 |
|
19 | 19 | import java.util.concurrent.TimeUnit;
|
20 | 20 |
|
21 |
| -import org.springframework.beans.factory.annotation.Autowired; |
22 | 21 | import org.springframework.boot.CommandLineRunner;
|
23 | 22 | import org.springframework.boot.SpringApplication;
|
24 | 23 | import org.springframework.boot.autoconfigure.SpringBootApplication;
|
25 | 24 | import org.springframework.boot.autoconfigure.mongo.MongoClientSettingsBuilderCustomizer;
|
26 | 25 | import org.springframework.context.annotation.Bean;
|
27 | 26 |
|
28 | 27 | @SpringBootApplication
|
29 |
| -public class SampleMongoApplication implements CommandLineRunner { |
| 28 | +public class SampleMongoApplication { |
30 | 29 |
|
31 |
| - @Autowired |
32 |
| - private CustomerRepository repository; |
33 |
| - |
34 |
| - @Override |
35 |
| - public void run(String... args) throws Exception { |
36 |
| - this.repository.deleteAll(); |
| 30 | + @Bean |
| 31 | + public CommandLineRunner exampleRunner(CustomerRepository repository) { |
| 32 | + return (args) -> { |
| 33 | + repository.deleteAll(); |
37 | 34 |
|
38 |
| - // save a couple of customers |
39 |
| - this.repository.save(new Customer("Alice", "Smith")); |
40 |
| - this.repository.save(new Customer("Bob", "Smith")); |
| 35 | + // save a couple of customers |
| 36 | + repository.save(new Customer("Alice", "Smith")); |
| 37 | + repository.save(new Customer("Bob", "Smith")); |
41 | 38 |
|
42 |
| - // fetch all customers |
43 |
| - System.out.println("Customers found with findAll():"); |
44 |
| - System.out.println("-------------------------------"); |
45 |
| - for (Customer customer : this.repository.findAll()) { |
46 |
| - System.out.println(customer); |
47 |
| - } |
48 |
| - System.out.println(); |
| 39 | + // fetch all customers |
| 40 | + System.out.println("Customers found with findAll():"); |
| 41 | + System.out.println("-------------------------------"); |
| 42 | + for (Customer customer : repository.findAll()) { |
| 43 | + System.out.println(customer); |
| 44 | + } |
| 45 | + System.out.println(); |
49 | 46 |
|
50 |
| - // fetch an individual customer |
51 |
| - System.out.println("Customer found with findByFirstName('Alice'):"); |
52 |
| - System.out.println("--------------------------------"); |
53 |
| - System.out.println(this.repository.findByFirstName("Alice")); |
| 47 | + // fetch an individual customer |
| 48 | + System.out.println("Customer found with findByFirstName('Alice'):"); |
| 49 | + System.out.println("--------------------------------"); |
| 50 | + System.out.println(repository.findByFirstName("Alice")); |
54 | 51 |
|
55 |
| - System.out.println("Customers found with findByLastName('Smith'):"); |
56 |
| - System.out.println("--------------------------------"); |
57 |
| - for (Customer customer : this.repository.findByLastName("Smith")) { |
58 |
| - System.out.println(customer); |
59 |
| - } |
| 52 | + System.out.println("Customers found with findByLastName('Smith'):"); |
| 53 | + System.out.println("--------------------------------"); |
| 54 | + for (Customer customer : repository.findByLastName("Smith")) { |
| 55 | + System.out.println(customer); |
| 56 | + } |
| 57 | + }; |
60 | 58 | }
|
61 | 59 |
|
62 | 60 | @Bean
|
|
0 commit comments