DemoController.java (1-21) package com.luv2code.springcoredemo; import…
- User
DemoController.java (1-21)
package com.luv2code.springcoredemo;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;
@RestController public class DemoController { private Coach myCoach;
code@Autowired public DemoController(Coach theCoach) { myCoach = theCoach; } @GetMapping("/dailyworkout") public String getDailyWorkout() { return myCoach.getDailyWorkout(); }}
CricketCoach.java (1-13)
package com.luv2code.springcoredemo;
import org.springframework.stereotype.Component;
@Component public class CricketCoach implements Coach{ @Override public String getDailyWorkout() { return "Practice fast bowling for 15 minutes"; } }
Coach.java (1-6)
package com.luv2code.springcoredemo;
public interface Coach { String getDailyWorkout(); }
I have a question regarding this code. It works perfectly, but my question it's. How it knows which Coach to use? For example in my code i have currently only one CricketCoach Component(why it's that a component?)
- Assistant