ConditionalOnBean without creating own config for every bean
I got a
@Configuration
that creates a bean, lets say Mandatory
, when a specific property is set. This bean is used by many other beans that are on other files and won't work without given bean. So I tried to add @ConditionalOnBean(value = [Mandatory::class])
on the classes that will need that bean. But that does not seem to work. The condition only worked when I created the beans manually inside of the same configuration.
Am I doing something wrong or do I really need to manually create all beans, when I wanna work with conditions?
10 Replies
⌛
This post has been reserved for your question.
Hey @Lord Tkay! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
Read this: https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.developing-auto-configuration.condition-annotations.bean-conditions
You need to be very careful about the order in which bean definitions are added, as these conditions are evaluated based on what has been processed so far. For this reason, we recommend using onlySo it's not necessarily that it's not working but you're trying to use autoconfig annotations in a regular component and the behaviour in that situation is undefined/not guaranteed@ConditionalOnBean
and@ConditionalOnMissingBean
annotations on auto-configuration classes (since these are guaranteed to load after any user-defined bean definitions have been added).
Unknown User•15mo ago
Message Not Public
Sign In & Join Server To View
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.
@piscean72 hmm what a bummer. So best practice would be to manually create all beans that should be conditionally inside of that config of mine?
This word, "manually". What do you think it means?
Creating a bean-method and creating the object instance explicit instead of letting spring creating the bean for me
Hm. I mean, in both cases, Spring does that because of annotaitons
Well, in the explicit version I would need to remove the
@Component
from the class I want to create a bean from
So it would look something like
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.