Kaynağa Gözat

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 yıl önce
ebeveyn
işleme
5e88da4f19
1 değiştirilmiş dosya ile 16 ekleme ve 4 silme
  1. 16
    4
      README.md

+ 16
- 4
README.md Dosyayı Görüntüle

@@ -108,8 +108,10 @@
108 108
 * Create a `PollController` class in the `controller` sub package.
109 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 117
 ### Part 3.1.1 - Create `GET` request method
@@ -273,8 +275,13 @@ public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
273 275
 ```java
274 276
 @RestController
275 277
 public class VoteController {
276
-    @Inject
278
+
277 279
     private VoteRepository voteRepository;
280
+    
281
+    @Autowired
282
+    public VoteController(VoteRepository voteRepository) {
283
+    	this.voteRepository = voteRepository;
284
+    }
278 285
 
279 286
     @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
280 287
     public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote
@@ -414,8 +421,13 @@ public class VoteResult {
414 421
 ```java
415 422
 @RestController
416 423
 public class ComputeResultController {
417
-    @Inject
424
+    
418 425
     private VoteRepository voteRepository;
426
+    
427
+    @Autowired
428
+    public ComputeResultController(VoteRepository voteRepository) {
429
+    	this.voteRepository = voteRepository;
430
+    }
419 431
 
420 432
     @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
421 433
     public ResponseEntity<?> computeResult(@RequestParam Long pollId) {