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(initMethod = "initialize")
@DependsOn("eventListener")
public EventPublisherBean eventPublisherBean () {
return new EventPublisherBean();
}

@Bean(name = "eventListener", initMethod = "initialize")
// @Lazy
public EventListenerBean eventListenerBean () {
return new EventListenerBean();
}

public static void main (String... strings) {
new AnnotationConfigApplicationContext(AppConfig.class);
}
}
Original Post




See Also