What does the @Resource annotation do, and what is the difference between it and @bean?

@ResourceAnnotations are like the courier of Java development, specially used to deliver dependencies to your code door. Its main job is to help you implement dependency injection and send other components (such as classes, objects, beans, etc.) to where you need them.

Specifically:

  1. Dependency injection: @Resource is responsible for injecting other components into the fields, methods or constructors you marked. It's like putting building blocks into your classes to make your code more fun and interesting.

  2. Specify the injected object: You can specify the name of the dependency you want to send in through the name attribute, just like specifying you when ordering takeout The name of the dish is the same.

  3. Match by name: If you do not specify a name, it will find the corresponding dependency based on the name of the field or method. Just like looking for a cat slave, if you call "meow", it will find a "meow" partner and send it to you.

  4. Match by type: If you don’t even want to worry about the name, just put @Resource and it will find a corresponding dependency based on the type. Of course, if there are multiple objects of the same type, you can add some information, such as @Qualifier annotation, to specify which one.

其实@ResourceIt is the "delivery boy" in your Java code, making dependencies clearer and the code cleaner. With it, your code is like living in a luxurious villa, with all kinds of services delivered to your door~

At this point someone will have questions. What is the difference between it and @bean?

@Bean and @Resource are two different ways to handle dependency injection. Specifically:

  • @Bean Annotation: You use @Bean to annotate the method in the configuration class. Spring will call this method and hand the returned object to the container, and then you You can get this Bean through @Autowired or ApplicationContext.

    @Configuration public class MyConfig { @Bean public MyService myService() { return new MyService(); } }

  • @Resource Annotation: is used on fields, methods or constructors to tell Spring to find a matching object in the container and inject it.

    public class MyService { @Resource private MyDao myDao; // 其他代码... }

So, both can implement dependency injection, and you can choose which method to use based on your preferences and project needs. Sometimes, @Bean is more suitable for defining some complex beans in configuration classes, while @Resource is more suitable for injecting simple dependencies in business classes.

If you use the @Bean annotation in a normal business class, Spring will not recognize it and cannot correctly register it as a Bean. Annotations such as @Autowired or @Resource are usually used in business classes for dependency injection.

So,@Bean annotations are mainly used on methods of configuration classes to help you register objects into the Spring container.

expand

  • @AutowiredIt is an annotation provided by the Spring framework and is used to implement dependency injection. It is one of Spring's more commonly used and powerful annotations.

  • Inject by type: Match by type. If there are multiple beans of the same type in the container, you can combine the @Qualifier annotation to specify the specific bean. .

  • Supports constructor injection: can be used on the constructor to inject dependencies through the constructor.

If you are in a pure Spring project, or if you do not have many restrictions on the choice of container, it is recommended to use @Autowired. It is part of Spring and is more flexible and powerful.

Guess you like

Origin blog.csdn.net/m0_64880608/article/details/135040107