user8914226
user8914226

Reputation:

Difference between Dependency Injection and @DependsOn

we are using Spring in our project and today I found, in my eyes, a strange beaviour. In my opinion Dependency Injection and the annotation @DependsOn should be the same in the following both cases

@Bean
public ClassA classA(){
    //code
}

@Bean
public ClassB classB(ClassA classA){
    someMethodWhichNeedsClassA()
}

and

@Bean
public ClassA classA(){
    //code
}

@Bean
@DependsOn("classA")
public ClassB classB(){
    someMethodWhichNeedsClassA()
}

It seems, that those both ways are not identical. I am not allowed to share my code, but in the first case I have access to a list which is filled in the first Bean, but not in the second case. I tried to find something in the documentation or in other blogs but without success.

Has anybody an idea if there is any differences between those both ways, because I thougth it has something to do with ordering of bean creation and thats all.

Thanks for any help

Upvotes: 1

Views: 1058

Answers (3)

Manjesh Kumar
Manjesh Kumar

Reputation: 11

In simple words there are two scenarios

  1. If beans creation is in sequence, spring will start initializing from top to bottom. In this case, depending on bean needs to listed first
  2. If beans creation is not in sequence and getting managed through multiple appconfig, in this we can use @DependsOn annotation to make sure that dependent beans are initialized before getting used.

Upvotes: 1

mentallurg
mentallurg

Reputation: 5207

If bean B calls any method on bean A (or passes to some other objects/methods that eventually call methods of bean A), then dependency injection is sufficient.

Bug there are cases when bean B does not need to call any methods on bean A, but dependsn on the result of initialization of bean A. Suppose bean A during its initialization creates tables in database, and bean B needs these table before it can be use. Then dependency injection makes no sense. Any code quality check will show you that injected variable is not used and thus is not needed. In such case you don't inject bean A into B, but declare dependency on bean A.

Briefly: If bean B calls bean A, use dependency injection. If bean B only needs results of initialization of bean A, use @DependsOn.

Upvotes: 0

Willem
Willem

Reputation: 1060

Spring, by default, manages beans' lifecycle and arranges their initialization order. This is the case in your first example. While in your second example you tell Spring that bean B depends on A. So spring will create bean A before bean B.

@DependsOn from spring docs:

Beans on which the current bean depends. Any beans specified are guaranteed to be created by the container before this bean. Used infrequently in cases where a bean does not explicitly depend on another through properties or constructor arguments, but rather depends on the side effects of another bean's initialization.

Baeldung spring DependsOn

Upvotes: 1

Related Questions