Showing posts with the label @DependsOn

Spring @DependsOn Annotation Example

In this section we will learn about @DependsOn  Annotation. The order in which Spring container creating beans cannot be predicted.  When we need spring to create certain bean before creating our bean, we can choose the @DependsOn annotation for managing bean creation order.  The @DependsOn annotation can force the Spring IoC container to initialize one or more beans before the bean which is annotated by @DependsOn annotation. The @DependsOn annotation may be used on any class annotated with @Component , @Service & @Repository or on methods annotated with @Bean . 1. Using @DependsOn on @Bean method @DependsOn annotation accepts array of bean names to specify the dependency beans. And again, here the order we keep the bean name in array is the order for bean creation. For the below code, the order of bean creation is twitterService , emailService , smsService . @Configuration public class AppConfig { @Bean @DependsOn ({ "twitterService" , "emailService&