Close

Spring Framework - @DependsOn Examples

Spring Framework 

The annotation DependsOn specifies (as it's String[] value() element) one or more beans to be loaded before this bean. We want to use this annotation when beans do not directly has references of each others but still depends on each other in some ways. Letting them initialize randomly by the Spring container might results in side effects.

@Configuration
@ComponentScan("com.logicbig.example")
public class AppConfig {

@Bean
@DependsOn("eventListenerBean")
public EventPublisher eventPublisherBean() {
return new EventPublisher();
}

@Bean
public EventListener eventListenerBean() {
return new EventListener();
}

@Bean
public EventManager eventManagerBean() {
return new EventManager();
}

public static void main(String... strings) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
context.close();

}
}
Original Post




See Also