Difference between @Bean and @Autowired
@Bean is used when you want to tell spring that this is the instance of this class. please give it me whenever I ask for istance of this class.
@Autowired is used to get the instance of the class which is annotated with the @Bean means you get the instance using the @Autowired.
you use @Bean for those classes object which you want to decide about how the object should be created for that respective class.
Example:
Class Bird({
String name;
@Bean
public Bird(){
name = "Sparrow";
}
}
Class Zoo{
@Autowired
private Bird bird;
}
So, here in the @Autowired will get the object from the bird class using the constructore annotated with the @Bean class.