Why can't I use @Autowired
in this case?
@SpringBootApplication public class Application { @Autowired BookingService bookingService; public static void main(String[] args) { bookingService.book("Alice", "Bob", "Carol"); } }
but can use @Bean
@SpringBootApplication public class Application { @Bean BookingService bookingService() { return new BookingService(); } public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(Application.class, args); BookingService bookingService = ctx.getBean(BookingService.class); bookingService.book("Alice", "Bob", "Carol"); } }
Aren't the two ways to generate BookingService
the same thing?
@Component is a class-level annotation, but @Bean is at the method level, so @Component is only an option when a class's source code is editable. @Bean can always be used, but it's more verbose. @Component is compatible with Spring's auto-detection, but @Bean requires manual class instantiation.
The @Autowired annotation provides more fine-grained control over where and how autowiring should be accomplished. The @Autowired annotation can be used to autowire bean on the setter method just like @Required annotation, constructor, a property or methods with arbitrary names and/or multiple arguments.
If we see component class like @Controller, @service, @repository will be scan automatically by the spring framework using the component scan. @Bean on the other hand can only be used to explicitly declare a single bean in a configuration class.
In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application.
@Bean BookingService bookingService() { return new BookingService(); }
Annotating @Bean
only registers the service as a bean(kind of an Object) in the spring application context. In simple words, it is just registration and nothing else.
@Autowired BookingService bookingService;
Annotating a variable with @Autowired
injects a BookingService
bean(i.e Object) from Spring Application Context.
(i.e) The registered object with @Bean
annotation will be injected to the variable annotated with @Autowired
.
Hope this clears your doubt!
@Bean
and @Autowired
do two very different things. The other answers here explain in a little more detail, but at a simpler level:
@Bean
tells Spring 'here is an instance of this class, please keep hold of it and give it back to me when I ask'.
@Autowired
says 'please give me an instance of this class, for example, one that I created with an @Bean
annotation earlier'.
Does that make sense? In your first example, you're asking Spring to give you an instance of BookingService
, but you're never creating one, so Spring has nothing to give you. In your second example, you're creating a new instance of BookingService
, telling Spring about it, and then, in the main()
method, asking for it back.
If you wanted, you could remove the two additional lines from the second main()
method, and combine your two examples as below:
@SpringBootApplication public class Application { @Autowired BookingService bookingService; @Bean BookingService bookingService() { return new BookingService(); } public static void main(String[] args) { bookingService.book("Alice", "Bob", "Carol"); } }
In this case, the @Bean
annotation gives Spring the BookingService
, and the @Autowired
makes use of it.
This would be a slightly pointless example, as you're using it all in the same class, but it becomes useful if you have the @Bean
defined in one class, and the @Autowired
in a different one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With