@Autowired notes and annotations with the use of @Qualifier ---- resolve to achieve more than selecting injection problems

Problem: When an interface is implemented by two implementation classes, only @Autowired annotation will be given, as shown in FIG.

Implementation class 1

 For Type 2

 controller injected

Then start the service being given, as follows:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'orderController': Unsatisfied dependency expressed through field 'productOrderService'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'net.xdclass.order_service.service.ProductOrderService' available: expected single matching bean but found 2: service1,service2

The reason is that there are two instances service1, service2, the system does not know which one example is injected, in fact, the tool has been rosy idea suggests that there are two instances, so here we need to use annotations to specify the instance @Qualifier injected, as shown below

So ok, idea prompted gains across the board is not, and also started normal

NOTE: We can also use @Resource (name = "service1") shown in FIG.

 

Guess you like

Origin www.cnblogs.com/matd/p/11128084.html