'Spring boot, @Autowired

i'm new in spring There's something I don't understand

Spring automatically register the class that implement interface, So we can code like below

@Service
public class MyServiceImpl implements MyService{
    @Override
    public String getHello( ){
        return "hello";
    }
}

@RestController
public class DIController{
    MyService myService;
    
    @Autowired
    public DIController(MyService myService){
        this.myService = myService;
    }
    
    @GetMapping("/hello")
    public String getHello( ){
        return myService.getHello( );
    }
}

instead of this

MyServiceImpl myServiceImpl;

However, what if there are several implementations? like this how we can register the Specific Class(ex. MyServiceImpl2)

@Service
public class MyServiceImpl1 implements MyService{
    @Override
    public String getHello( ){
        return "hello";
    }
}

@Service
public class MyServiceImpl2 implements MyService{
    @Override
    public String getHello( ){
        return "hello";
    }
}

thanks a lot!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source