A small discovery in making the wheel
When an interface is implemented by two services, the controller calls the interface implementation function, and an error will be reported, prompting the developer to specify the service. Officially, it is recommended that you use @Qualifier to distinguish, but there is always another way to achieve it.
Scenes
public interface Hi{ String hi(); } @Service public class Hi1 implements Hi{ @Override public String hi() { return "hi1"; } } @Service public class Hi2 implements Hi{ @Override public String hi() { return "hi2"; } }
@Controller
public class SingerService { @Autowired privateHi hi; public String sing(){ return hi.hi(); } }
If no specific interface implementation class is specified at this time, spring will report an error, but as long as Hi2 is changed to
@Service
@Primary public class Hi2 implements Hi{ @Override public String hi() { return "hi2"; } }
The command line will output
hi2