Issue
For Bean creation in SpringBoot, we use class annotated with the @Component with some bean creation methods annotated with @Bean annotation. Now, I have always been using @Bean like this:
@Bean
public func getSome() {
return someFunc(param1, param2, param3);
}
Now, what I saw in some code is this:
@Bean
public func getSome(Type1 param1, Type2 param2, Type 3 param3) {
return someFunc(param1, param2, param3);
}
So basically, Beans are created when the SpringBoot context loads. What I am confused here is how will SpringBoot pick up the parameters in the bean (the second example) .
Can someone please help me understand this ?
PS: Please let me know if the question is not clear. :)
Solution
Spring will do standard lookup for Type1, Type2, Type3 types beans in the context. If those are not found, Spring will try to create new instances using default constructor. If no default constructor, startup will fail.
Probably there are some more mechanics, but basics is this.
Answered By - ILya Cyclone
Answer Checked By - David Marino (JavaFixing Volunteer)