Skip to content

Commit f0e68fb

Browse files
committed
Fixup constructor injections
Consistently use constructor injection and remove superfluous @Autowired annotations.
1 parent 9eeb536 commit f0e68fb

File tree

24 files changed

+105
-81
lines changed

24 files changed

+105
-81
lines changed

Diff for: livelessons-choreography/livelessons-choreography-bookmark-service/src/main/java/demo/BookmarkRestController.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.Collection;
44

5-
import org.springframework.beans.factory.annotation.Autowired;
65
import org.springframework.web.bind.annotation.PathVariable;
76
import org.springframework.web.bind.annotation.RequestBody;
87
import org.springframework.web.bind.annotation.RequestMapping;
@@ -13,8 +12,11 @@
1312
@RequestMapping("/{userId}/bookmarks")
1413
public class BookmarkRestController {
1514

16-
@Autowired
17-
private BookmarkRepository bookmarkRepository;
15+
private final BookmarkRepository bookmarkRepository;
16+
17+
public BookmarkRestController(BookmarkRepository bookmarkRepository) {
18+
this.bookmarkRepository = bookmarkRepository;
19+
}
1820

1921
@RequestMapping(method = RequestMethod.GET)
2022
public Collection<Bookmark> getBookmarks(@PathVariable String userId) {

Diff for: livelessons-choreography/livelessons-choreography-contact-service/src/main/java/demo/ContactRestController.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.Collection;
44

5-
import org.springframework.beans.factory.annotation.Autowired;
65
import org.springframework.web.bind.annotation.PathVariable;
76
import org.springframework.web.bind.annotation.RequestBody;
87
import org.springframework.web.bind.annotation.RequestMapping;
@@ -13,8 +12,11 @@
1312
@RequestMapping("/{userId}/contacts")
1413
public class ContactRestController {
1514

16-
@Autowired
17-
private ContactRepository contactRepository;
15+
private final ContactRepository contactRepository;
16+
17+
public ContactRestController(ContactRepository contactRepository) {
18+
this.contactRepository = contactRepository;
19+
}
1820

1921
@RequestMapping(method = RequestMethod.GET)
2022
public Collection<Contact> getContacts(@PathVariable String userId) {

Diff for: livelessons-choreography/livelessons-choreography-gateway/src/main/java/demo/PassportRestController.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import reactor.rx.Stream;
44

5-
import org.springframework.beans.factory.annotation.Autowired;
65
import org.springframework.web.bind.annotation.PathVariable;
76
import org.springframework.web.bind.annotation.RequestMapping;
87
import org.springframework.web.bind.annotation.RestController;
@@ -11,8 +10,11 @@
1110
@RestController
1211
public class PassportRestController {
1312

14-
@Autowired
15-
private PassportService passportService;
13+
private final PassportService passportService;
14+
15+
public PassportRestController(PassportService passportService) {
16+
this.passportService = passportService;
17+
}
1618

1719
@RequestMapping("/{userId}/passport")
1820
public DeferredResult<Passport> passport(@PathVariable String userId) {

Diff for: livelessons-choreography/livelessons-choreography-gateway/src/main/java/demo/PassportService.java

+10-7
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@
44
import reactor.rx.Stream;
55
import reactor.rx.Streams;
66

7-
import org.springframework.beans.factory.annotation.Autowired;
87
import org.springframework.stereotype.Component;
98

109
@Component
1110
public class PassportService {
1211

13-
@Autowired
14-
private Environment environment;
12+
private final Environment environment;
1513

16-
@Autowired
17-
private ContactClient contactClient;
14+
private final ContactClient contactClient;
1815

19-
@Autowired
20-
private BookmarkClient bookmarkClient;
16+
private final BookmarkClient bookmarkClient;
17+
18+
public PassportService(Environment environment, ContactClient contactClient,
19+
BookmarkClient bookmarkClient) {
20+
this.environment = environment;
21+
this.contactClient = contactClient;
22+
this.bookmarkClient = bookmarkClient;
23+
}
2124

2225
public Stream<Bookmark> getBookmarks(String userId) {
2326
return Streams.<Bookmark>create(subscriber -> {

Diff for: livelessons-choreography/livelessons-choreography-hystrix/src/main/java/demo/IntegrationClient.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@
55

66
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
77

8-
import org.springframework.beans.factory.annotation.Autowired;
98
import org.springframework.stereotype.Component;
109

1110
@Component
1211
class IntegrationClient {
1312

14-
@Autowired
15-
private ContactClient contactClient;
13+
private final ContactClient contactClient;
1614

17-
@Autowired
18-
private BookmarkClient bookmarkClient;
15+
private final BookmarkClient bookmarkClient;
16+
17+
public IntegrationClient(ContactClient contactClient, BookmarkClient bookmarkClient) {
18+
this.contactClient = contactClient;
19+
this.bookmarkClient = bookmarkClient;
20+
}
1921

2022
public Collection<Bookmark> getBookmarksFallback(String userId) {
2123
System.out.println("getBookmarksFallback");
@@ -37,4 +39,4 @@ public Collection<Contact> getContacts(String userId) {
3739
return this.contactClient.getContacts(userId);
3840
}
3941

40-
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
package demo;
22

3-
import org.springframework.beans.factory.annotation.Autowired;
43
import org.springframework.web.bind.annotation.PathVariable;
54
import org.springframework.web.bind.annotation.RequestMapping;
65
import org.springframework.web.bind.annotation.RestController;
76

87
@RestController
98
class PassportRestController {
109

11-
@Autowired
12-
private IntegrationClient integrationClient;
10+
private final IntegrationClient integrationClient;
11+
12+
public PassportRestController(IntegrationClient integrationClient) {
13+
this.integrationClient = integrationClient;
14+
}
1315

1416
@RequestMapping("/{userId}/passport")
1517
Passport passport(@PathVariable String userId) {
1618
return new Passport(userId, this.integrationClient.getContacts(userId),
1719
this.integrationClient.getBookmarks(userId));
1820
}
1921

20-
}
22+
}

Diff for: livelessons-choreography/livelessons-choreography-ribbon-and-eureka/src/main/java/demo/DiscoveryClientExample.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import org.apache.commons.lang.builder.ToStringBuilder;
44

5-
import org.springframework.beans.factory.annotation.Autowired;
65
import org.springframework.boot.CommandLineRunner;
76
import org.springframework.cloud.client.ServiceInstance;
87
import org.springframework.cloud.client.discovery.DiscoveryClient;
@@ -13,8 +12,11 @@
1312
@Component
1413
public class DiscoveryClientExample implements CommandLineRunner {
1514

16-
@Autowired
17-
private DiscoveryClient discoveryClient;
15+
private final DiscoveryClient discoveryClient;
16+
17+
public DiscoveryClientExample(DiscoveryClient discoveryClient) {
18+
this.discoveryClient = discoveryClient;
19+
}
1820

1921
@Override
2022
public void run(String... strings) throws Exception {

Diff for: livelessons-choreography/livelessons-choreography-ribbon-and-eureka/src/main/java/demo/FeignExample.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package demo;
22

3-
import org.springframework.beans.factory.annotation.Autowired;
43
import org.springframework.boot.CommandLineRunner;
54
import org.springframework.core.annotation.Order;
65
import org.springframework.stereotype.Component;
@@ -9,11 +8,14 @@
98
@Component
109
public class FeignExample implements CommandLineRunner {
1110

12-
@Autowired
13-
private ContactClient contactClient;
11+
private final ContactClient contactClient;
1412

15-
@Autowired
16-
private BookmarkClient bookmarkClient;
13+
private final BookmarkClient bookmarkClient;
14+
15+
public FeignExample(ContactClient contactClient, BookmarkClient bookmarkClient) {
16+
this.contactClient = contactClient;
17+
this.bookmarkClient = bookmarkClient;
18+
}
1719

1820
@Override
1921
public void run(String... strings) throws Exception {

Diff for: livelessons-choreography/livelessons-choreography-ribbon-and-eureka/src/main/java/demo/RestTemplateExample.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.List;
44

5-
import org.springframework.beans.factory.annotation.Autowired;
65
import org.springframework.boot.CommandLineRunner;
76
import org.springframework.core.ParameterizedTypeReference;
87
import org.springframework.core.annotation.Order;
@@ -15,8 +14,11 @@
1514
@Component
1615
public class RestTemplateExample implements CommandLineRunner {
1716

18-
@Autowired
19-
private RestTemplate restTemplate;
17+
private final RestTemplate restTemplate;
18+
19+
public RestTemplateExample(RestTemplate restTemplate) {
20+
this.restTemplate = restTemplate;
21+
}
2022

2123
@Override
2224
public void run(String... strings) throws Exception {

Diff for: livelessons-cloud/livelessons-cloud-connectors/src/main/java/demo/ExampleController.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package demo;
22

3-
import org.springframework.beans.factory.annotation.Autowired;
43
import org.springframework.cloud.app.ApplicationInstanceInfo;
54
import org.springframework.jdbc.core.JdbcTemplate;
65
import org.springframework.web.bind.annotation.RequestMapping;
@@ -13,20 +12,20 @@ public class ExampleController {
1312

1413
private final ApplicationInstanceInfo info;
1514

16-
@Autowired
1715
public ExampleController(JdbcTemplate jdbc, ApplicationInstanceInfo info) {
1816
this.jdbc = jdbc;
1917
this.info = info;
2018
}
2119

2220
@RequestMapping("/")
2321
public String hello() {
24-
return jdbc.queryForObject("select model from car where id = 1", String.class);
22+
return this.jdbc.queryForObject("select model from car where id = 1",
23+
String.class);
2524
}
2625

2726
@RequestMapping("/cloudinfo")
2827
public ApplicationInstanceInfo info() {
29-
return info;
28+
return this.info;
3029
}
3130

3231
}

Diff for: livelessons-cloud/livelessons-cloud-s3/src/main/java/demo/CloudS3Application.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import javax.annotation.PostConstruct;
88

9-
import org.springframework.beans.factory.annotation.Autowired;
109
import org.springframework.beans.factory.annotation.Value;
1110
import org.springframework.boot.SpringApplication;
1211
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -23,12 +22,15 @@ public class CloudS3Application {
2322
@Value("${livelessons.s3.bucket}")
2423
private String bucket;
2524

26-
@Autowired
27-
public ResourceLoader resourceLoader;
25+
private final ResourceLoader resourceLoader;
26+
27+
public CloudS3Application(ResourceLoader resourceLoader) {
28+
this.resourceLoader = resourceLoader;
29+
}
2830

2931
@PostConstruct
3032
public void resourceAccess() throws IOException {
31-
String location = "s3://" + bucket + "/file.txt";
33+
String location = "s3://" + this.bucket + "/file.txt";
3234
WritableResource writeableResource = (WritableResource) this.resourceLoader
3335
.getResource(location);
3436
FileCopyUtils.copy("Hello World!",

Diff for: livelessons-cloud/livelessons-cloud-services/src/main/java/demo/ExampleController.java

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package demo;
22

3-
import org.springframework.beans.factory.annotation.Autowired;
43
import org.springframework.jdbc.core.JdbcTemplate;
54
import org.springframework.web.bind.annotation.RequestMapping;
65
import org.springframework.web.bind.annotation.RestController;
@@ -10,7 +9,6 @@ public class ExampleController {
109

1110
private final JdbcTemplate jdbc;
1211

13-
@Autowired
1412
public ExampleController(JdbcTemplate jdbc) {
1513
this.jdbc = jdbc;
1614
}

Diff for: livelessons-data/livelessons-data-jdbc/src/main/java/demo/CarRepository.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.List;
44

5-
import org.springframework.beans.factory.annotation.Autowired;
65
import org.springframework.jdbc.core.JdbcTemplate;
76
import org.springframework.stereotype.Repository;
87

@@ -11,13 +10,12 @@ public class CarRepository {
1110

1211
private final JdbcTemplate jdbc;
1312

14-
@Autowired
1513
public CarRepository(JdbcTemplate jdbc) {
1614
this.jdbc = jdbc;
1715
}
1816

1917
public List<Car> findByMakeIgnoringCase(String make) {
20-
return jdbc.query(
18+
return this.jdbc.query(
2119
"select * from car " + "where UPPER(car.make) = UPPER(?) order by id",
2220
(rs, i) -> new Car(rs.getInt("id"), rs.getString("make"),
2321
rs.getString("model"), rs.getInt("year")),

Diff for: livelessons-data/livelessons-data-redis/src/main/java/demo/Demo.java

-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import org.apache.commons.logging.Log;
77
import org.apache.commons.logging.LogFactory;
88

9-
import org.springframework.beans.factory.annotation.Autowired;
109
import org.springframework.boot.CommandLineRunner;
1110
import org.springframework.data.redis.core.StringRedisTemplate;
1211
import org.springframework.data.redis.core.ValueOperations;
@@ -22,7 +21,6 @@ public class Demo implements CommandLineRunner {
2221

2322
private final SlowService service;
2423

25-
@Autowired
2624
public Demo(StringRedisTemplate template, SlowService service) {
2725
this.template = template;
2826
this.service = service;

Diff for: livelessons-integration/livelessons-integration-basic-messaging/src/main/java/demo/Producer.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
import java.util.UUID;
77

88
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
9-
import org.springframework.beans.factory.annotation.Autowired;
109
import org.springframework.boot.CommandLineRunner;
1110
import org.springframework.stereotype.Component;
1211

1312
@Component
1413
public class Producer implements CommandLineRunner {
1514

16-
@Autowired
17-
private RabbitMessagingTemplate messagingTemplate;
15+
private final RabbitMessagingTemplate messagingTemplate;
16+
17+
public Producer(RabbitMessagingTemplate messagingTemplate) {
18+
this.messagingTemplate = messagingTemplate;
19+
}
1820

1921
@Override
2022
public void run(String... args) throws Exception {

Diff for: livelessons-integration/livelessons-integration-batch/src/main/java/demo/ContactBatchJobConfiguration.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
1717
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
1818
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
19-
import org.springframework.beans.factory.annotation.Autowired;
2019
import org.springframework.boot.autoconfigure.batch.JobExecutionEvent;
2120
import org.springframework.context.ApplicationListener;
2221
import org.springframework.context.annotation.Bean;
@@ -88,13 +87,16 @@ public JdbcTemplate jdbcTemplate(DataSource dataSource) {
8887
public static class BatchJobFinishedListener
8988
implements ApplicationListener<JobExecutionEvent> {
9089

91-
@Autowired
92-
private JdbcTemplate jdbcTemplate;
90+
private final JdbcTemplate jdbcTemplate;
91+
92+
public BatchJobFinishedListener(JdbcTemplate jdbcTemplate) {
93+
this.jdbcTemplate = jdbcTemplate;
94+
}
9395

9496
@Override
9597
public void onApplicationEvent(JobExecutionEvent event) {
9698
System.out.println("finished " + event.getJobExecution().toString());
97-
jdbcTemplate
99+
this.jdbcTemplate
98100
.query("SELECT first_name, last_name, email FROM contact",
99101
(rs, i) -> new Contact(rs.getString("first_name"),
100102
rs.getString("last_name"), rs.getString("email")))

0 commit comments

Comments
 (0)