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?
@Configuration
class MyConfig() {
@Bean
@ConditionalOnProperty(["my.property.activate"])
fun mandatory(): Mandatory {
return Mandatory()
}

// This works
@Bean
@ConditionalOnBean(value = [Mandatory::class])
fun someClass(mandatory: Mandatory): SomeClass {
return SomeClass(mandatory)
}
}
@Configuration
class MyConfig() {
@Bean
@ConditionalOnProperty(["my.property.activate"])
fun mandatory(): Mandatory {
return Mandatory()
}

// This works
@Bean
@ConditionalOnBean(value = [Mandatory::class])
fun someClass(mandatory: Mandatory): SomeClass {
return SomeClass(mandatory)
}
}
// This does not work
@Component
@ConditionalOnBean([Mandatory::class])
class SomeClass(var mandatory: Mandatory) {
//...
}
// This does not work
@Component
@ConditionalOnBean([Mandatory::class])
class SomeClass(var mandatory: Mandatory) {
//...
}
10 Replies
JavaBot
JavaBot15mo ago
This post has been reserved for your question.
Hey @Lord Tkay! Please use /close or the Close 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.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
imp_o_rt
imp_o_rt15mo ago
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 only @ConditionalOnBean and @ConditionalOnMissingBean annotations on auto-configuration classes (since these are guaranteed to load after any user-defined bean definitions have been added).
So 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
Unknown User
Unknown User15mo ago
Message Not Public
Sign In & Join Server To View
JavaBot
JavaBot15mo ago
💤 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.
Lord Tkay
Lord TkayOP15mo ago
@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?
Kyo-chan
Kyo-chan15mo ago
This word, "manually". What do you think it means?
Lord Tkay
Lord TkayOP15mo ago
Creating a bean-method and creating the object instance explicit instead of letting spring creating the bean for me
Kyo-chan
Kyo-chan15mo ago
Hm. I mean, in both cases, Spring does that because of annotaitons
Lord Tkay
Lord TkayOP15mo ago
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
@Configuration
class MyConfig() {
@Bean
@ConditionalOnProperty(["my.property.activate"])
fun mandatory(): Mandatory {
return Mandatory()
}

@Bean
@ConditionalOnBean(value = [Mandatory::class])
fun someClass(mandatory: Mandatory): SomeClass {
return SomeClass(mandatory)
}
}

class SomeClass(var mandatory: Mandatory) {
//...
}
@Configuration
class MyConfig() {
@Bean
@ConditionalOnProperty(["my.property.activate"])
fun mandatory(): Mandatory {
return Mandatory()
}

@Bean
@ConditionalOnBean(value = [Mandatory::class])
fun someClass(mandatory: Mandatory): SomeClass {
return SomeClass(mandatory)
}
}

class SomeClass(var mandatory: Mandatory) {
//...
}
JavaBot
JavaBot15mo ago
💤 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.

Did you find this page helpful?