This may happen when two beans have same names.
Module1Beans.java
:
@Configuration
public class Module1Beans {
@Bean
public GoogleAPI retrofitService(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://www.google.com/")
.addConverterFactory(JacksonConverterFactory.create())
.build();
return retrofit.create(GoogleAPI.class);
}
}
Module2Beans.java
:
@Configuration
public class Module2Beans {
@Bean
public GithubAPI retrofitService(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://www.github.com/")
.addConverterFactory(JacksonConverterFactory.create())
.build();
return retrofit.create(GithubAPI.class);
}
}
A bean named retrofitService
is first created, and it's type is GoogleAPI
, then covered by a GithubAPI
becauce they're both created by a retrofitService()
method.
Now when you @Autowired
a GoogleAPI
you'll get a message like Field googleAPI in com.example.GoogleService required a bean of type 'com.example.rest.GoogleAPI' that could not be found.