Java Interface Extension and Spring's Autowired

Shazam :

I have this interface:

public interface RestService {
    //other methods
}

and this interface which extends RestService:

public interface NewRestService extends RestService{
        void downloadPOD(String baseUrl, String orderNo);   
}

My Implementation:

public class NewRestServiceImpl implements NewRestService { 
   public void downloadPOD(...);

   //other method implementations
}

When I do this, it says

downloadPOD()is undefined:

@Autowired
private RestService restService;
...
restService.downloadPOD(...)

Please enlighten me. Thanks

Andronicus :

It's because downloadPOD method is not defined in the RestService interface. If you're sure the right implementation will be injected, you can cast the bean:

@Autowired
private RestService restService;

((NewRestService) restService).downloadPOD(...)

Or if you need to use the api of the implementation, that is not in the interface, maybe it would be better to simply inject NewRestService or NewRestServiceImpl:

@Autowired
private NewRestService newRestService;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=334623&siteId=1