Browse Source

Update the README to suggest constructor-based DI

Constructor-based DI asserts that the class cannot
be instantiated without the supplied dependency. This method
enforces the requirement whether or not you use Spring, and
therefore is container-agnostic.

Also, Spring will throw up a Warning flag if you just annotate
the prop with @Inject
vvmk 6 years ago
parent
commit
5e88da4f19
1 changed files with 16 additions and 4 deletions
  1. 16
    4
      README.md

+ 16
- 4
README.md View File

108
 * Create a `PollController` class in the `controller` sub package.
108
 * Create a `PollController` class in the `controller` sub package.
109
 	* `PollController` signature should be `annotated` with `@RestController`
109
 	* `PollController` signature should be `annotated` with `@RestController`
110
 
110
 
111
-* `PollController` has a `pollRepository` instance variable of type `PollRepository`
112
-	* `pollRepository` should be `annotated` with `@Inject`
111
+* `PollController` has a `pollRepository` instance variable of type `PollRepository`  
112
+
113
+* Create a constructor that accepts a `PollRepository` argument and assigns its value to the `pollRepository` member variable.  
114
+	* Mark the constructor with the `@Autowired` annotation.
113
 
115
 
114
 
116
 
115
 ### Part 3.1.1 - Create `GET` request method
117
 ### Part 3.1.1 - Create `GET` request method
273
 ```java
275
 ```java
274
 @RestController
276
 @RestController
275
 public class VoteController {
277
 public class VoteController {
276
-    @Inject
278
+
277
     private VoteRepository voteRepository;
279
     private VoteRepository voteRepository;
280
+    
281
+    @Autowired
282
+    public VoteController(VoteRepository voteRepository) {
283
+    	this.voteRepository = voteRepository;
284
+    }
278
 
285
 
279
     @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
286
     @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
280
     public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote
287
     public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote
414
 ```java
421
 ```java
415
 @RestController
422
 @RestController
416
 public class ComputeResultController {
423
 public class ComputeResultController {
417
-    @Inject
424
+    
418
     private VoteRepository voteRepository;
425
     private VoteRepository voteRepository;
426
+    
427
+    @Autowired
428
+    public ComputeResultController(VoteRepository voteRepository) {
429
+    	this.voteRepository = voteRepository;
430
+    }
419
 
431
 
420
     @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
432
     @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
421
     public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
433
     public ResponseEntity<?> computeResult(@RequestParam Long pollId) {