#Spring autowires "higher level" bean instead of throwing an error

1 messages · Page 1 of 1 (latest)

quasi pewter
#

In my current code base I'm working on we have

//Config.java
@Bean
@Profile(ProfileRegistry.LOCAL_OR_TEST)
public ApvcBlobClient apvcLocalAzureBlobClient() {
  return new LocalApvcBlobClient();
}

// LocalApvcBlobClient.java
public class LocalApvcBlobClient extends LocalBlobClient implements ApvcBlobClient {
}

//test folder / SpringTest.java
@Autowired
LocalApvcBlobClient apvcBlobClient;

I know you can inject more concrete implementations into higher interfaces. Which would mean I can inject a LocalApvcBlobClient bean into a ApvcBlobClient variable. But the current code does does not have any bean for the LocalApvcBlobClient implementation. What confuses me is, that IntelliJ shows an error Could not autowire. No beans of 'LocalApvcBlobClient' type found. and thus cant navigate to the bean. Does this mean, spring looks at the returned object type instead of the declared method return type to determine in what this can be injected?

pine furnaceBOT
#

<@&1004656351647117403> please have a look, thanks.

quasi pewter
#

Spring autowires "higher level" bean instead of throwing an error

clear dove
#

hw, why does that confuse you? you said yourself that your code contains no bean definitions for LocalApvcBlobClient

#

also, idea's assumptions can be wrong, for example I have a case where interface implementations are split over multiple modules and idea gets a bit thrown off and tells me there are multiple definitions when in fact there is always only one

quasi pewter
# clear dove hw, why does that confuse you? you said yourself that your code contains no bean...

If spring would look at the method header return type, this would be like in the image. It casts the value to the appropriate type and some functions may not be available. So my guess is spring looks at the datatype of the object that is being returned rather than the method header return type. Then another question is, does the method header return type affect anything in spring? It could also be Object

clear dove
#

I don't quite get the context

#

does your test run successfully?

quasi pewter
#

Yes. I haven't written this code but my team, but I thought this is pretty weird, especially because IntelliJ shows this warning

clear dove
#

yeah, Spring does a lot of inspection of the beans

#

Idea won't analyze code like that

#

it's easier to do this at runtime that to analyze code

quasi pewter
#

And LocalApvcBlobClient is only once ever created in the code base -> in this bean

#

But do you know for sure that spring is looking at the datatype of the object that is being returned rather than the method header return type? This is basically the answer what I'm looking for. For now this seems the only logical reason to me how this could even work.

clear dove
#

yes, it is looking at the actual type of the bean