@ConditionalOnBean annotation in Spring Boot

I introduced to you the @Conditional annotation in Spring with the purpose of allowing us to define conditions for a bean to be initialized in the Spring container. We will need to implement the Condition interface with the abstract method matches() returning true or false to represent whether or not these conditions are met. Spring Boot provides us with a number of implementations for the Condition interface along with corresponding annotations that help us solve many practical problems when working with Spring Boot applications. One of them is the @ConditionalOnBean annotation.

Spring Boot’s @ConditionalOnBean annotation is used to let Spring know that the class or method annotated with this annotation can only initialize the bean in the Spring container if a bean of another class already exists in the Spring container.

For example, I have 2 simple classes as follows:


To configure beans for the above classes in the Spring container, I will define an annotated class with the @Configuration annotation as follows:

Now, if you get the Student information defined in the Spring container:

you will see the following results:

If I now want the Student class bean to be initialized only if the Clazz class bean already exists in the Spring container, I can annotate the method that defines the Student class bean in the ApplicationConfiguration class as follows:

In case the Spring container has not yet defined a bean for the Clazz class:

then you will see the following error when running again for example:

If you define a bean for the Clazz class:

then you won’t see the error anymore.

If you take a look at the content of the @ConditionalOnBean annotation, you will see that this annotation uses the @Conditional annotation along with the implementation of the Condition interface, OnBeanCondition:

You can read the code of the OnBeanCondition class if you want to learn more about how Spring Boot implements this condition!

As you can see, in addition to using the class name to declare in the @ConditionalOnBean annotation, we can also use a number of other ways to declare conditions. For example, we can use the name of the bean in the Spring container to declare it in the name attribute of the @ConditionalOnBean annotation as follows:

Add Comment