As you can see in this example:
@SpringBootApplication
public class KafkaTestApplication {
public static void main(String[] args) {
SpringApplication.run(KafkaTestApplication.class, args);
}
@Configuration
public static class ProjectConfig {
@Bean
public MessageService smsService() {
System.out.println("SMS service");
return new SmsService();
}
@Bean
public String something(SmsService smsService) {
System.out.println("Something");
return smsService.toString();
}
public static class SmsService implements MessageService {
public SmsService() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public interface MessageService {}
}
}
Spring is somehow resolving SmsService even though it doesn't have a bean of type SmsService what it dos have is MessageService. I tried delaying the construction of something bean with that thread sleep method, but every time smsService bean is constructed first. Also intellij gives a warning in there Could not autowire. No beans of 'SmsService' type found. which makes sense but the whole thing just compiles fine.
Is this behavior guaranteed to always work like that? Can one rely on returning object in it's parent form and expect spring to inject it as a child form? Very strange.