The difference between @Bean and @Autowired and @Resource
The @Bean modified method means initializing an object and handing it over to Spring IOC for management. @Bean can only be used with @Component @Repository @Controller @Service @Configration.
@Autowired modifies variables and methods for completing automatic assembly (injecting required external resources)
@Resource is an annotation provided by java, similar to @Autowired
The difference between @Bean and @Autowired
I saw a question on the website:
Why can't @Autowired be used in this case?
@SpringBootApplication public class Application { @Autowired BookingService bookingService; public static void main(String[] args) { ("Alice", "Bob", "Carol"); } }
But you can use @Bean
@SpringBootApplication public class Application { @Bean BookingService bookingService() { return new BookingService(); } public static void main(String[] args) { ApplicationContext ctx = (, args); BookingService bookingService = (); ("Alice", "Bob", "Carol"); } }
the difference:
Briefly explain:
-
@Bean
Tell Spring's this is an instance of this class, please keep that class and return it to me when I ask'. -
@Autowired
Say "Please give me an instance of the class, for example, an instance I created with annotations before @Bean".
Does that make sense? In the first example, you want Spring to give you an instance BookingService, but you never created an instance, so Spring has nothing to give you. In the second example, you will create a new instance of BookingService, inform Spring, and then ask it to return in the main() method.
If necessary, you can delete the other two lines from the second main() method and combine the following two examples:
@SpringBootApplication public class Application { @Autowired BookingService bookingService; @Bean BookingService bookingService() { return new BookingService(); } public static void main(String[] args) { ("Alice", "Bob", "Carol"); } }
in this case,@Bean
Comments provide SpringBookingService
, and@Autowired
use.
This would be a pointless example because you are all using them in the same class, but if you@Bean
Defined the class in one class and defined it in another class, it will become useful@Autowired
。
The difference between @Resource and @Autowired
- @Autowired will first press byType to find it. If it is not found, it will follow byName to find it.
- @Resource will first press byName to look for it, and if it is not found, it will be byType to look for it. If the name attribute is set, you will only press byName to search, and if you cannot find it, an error will be reported.
@Resource(name = "studentServiceImpl") private PersonService personService;
When to use Autowired and Resource?
-
Autowired
: Inefficient, search by type first, then search by name -
Resource
: Look up by name, followed by the parameter name. Benefits: When there are multiple Impl implementation classes, you can quickly find it through name.
When there is only one Impl real class, it is almost the same to use any one. When >=2, it is best to use @Resource, which is more efficient than @Autowired@Qualifier()
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.