I find that many of my friends still struggle with these questions when coding. Should I use @Autowired or @Resource for dependency injection of beans? Why is the project code used differently by my colleagues than it does by me? Who is right and who is wrong, and how will different injection positions affect the project?

For those of you who have used Spring, there are usually set injection, constructor injection, static factory injection, and instance factory injection when configuring beans based on XML. You can learn these methods by yourself because they are tedious and complex. This article only explores annotation-based use.

Either @AutoWired, @Resource, or @Inject can help manage bean implementation dependency injection, and they all help simplify bean configuration. The former two are the most used in daily development, accounting for 90% of the total.

@Autowired

First, this annotation, provided by Spring, defaults to finding the corresponding bean based on its type (the implementation class of the type of the dependent bean). In the following code snippet, when users make online payments, they need to rely on the PayService payment interface, which is assumed to have one and only one specific implementation. It doesn’t matter what the implementation is, it matters that there is only one annotation, so there is no problem with just one annotation via @Autowired.

For example, at lunchtime, when you walk into a dumpling restaurant that only sells one kind of dumpling (let’s say leek and egg, because I love leek and egg, YYds!). You say to the boss, the boss to a dumpling! Even if you don’t say the exact filling, the boss will know what to serve you because there is only one kind of dumpling and he will serve you that one by default!

In this example, you = the interface caller, dumpling house = interface (offering to sell dumplings), leek egg stuffing = interface concrete implementation (unique implementation).

@RestControllerpublic class PayController { @Autowired private PayService payService; @RequestMapping(value = "/pay", method = RequestMethod.GET) public String pay(){ return payService.pay(); }}Copy the code

However, when PayService has more than two implementations, it is not possible to inject beans via @autoWired. An error will be reported when the project is started. Why? Because Spring can’t, it doesn’t know which implementation to inject.

As the above example, the dumpling house sells many kinds of dumplings, such as: egg chives, corn pork, celery beef and so on. You say to the boss a dumpling, the boss does not know you want to eat exactly what kind of dumpling, you need to describe exactly which kind.

@Qualifier

This annotation is used to specify which implementation of the bean to inject, which tells the dumpling owner exactly what filling you want to eat. This annotation is common in scenarios where there are multiple implementations of an interface, including one implementation. * * * *

Examples are as follows: Note that the implementation of the interface is specified through @Qualifier(“weChatPayServiceImpl”) when multiple payment methods exist. So Spring can do the injection for us.

@RestControllerpublic 
class PayController {

    @Autowired    
    @Qualifier("weChatPayServiceImpl")    
    private PayService payService;
    
    @RequestMapping(value = "/pay", method = RequestMethod.GET)   
    public String pay(){        
    return payService.pay();    
    }
}
Copy the code

@Resource

This annotation can also help with bean injection, but it’s not exactly the same as @AutoWired. First, it is not an official spring injection method. Second, it is important to note that by default it looks for beans by name, which means it looks for implementations by the name of the property you are injecting.

The two code snippets below do the same thing

@Resource 
private PayService weChatPayServiceImpl;
Copy the code
@Resource("weChatPayServiceImpl")
private PayService payService;
Copy the code

Similarly, when there are multiple bean implementations, either using @AutoWired or @Resource to help us inject, the specific implementation can be specified through the ** @qualifier annotation along with the injection.

Therefore, using @autowired or @Resource can help us manage beans. Either way, you simplify the configuration of XML, and you can use both in your work. There is no right or wrong way to use them, just pay attention to what you need to do when using the corresponding annotations.

In addition, many friends only know that they can only apply to field attributes, in fact, there are many ways to inject, such as constructor injection, set injection, etc., specific scope can refer to the annotation scope.