The ambiguity of automatic assembly in Spring combat

Autowiring only works when only one bean matches the desired result. This ambiguity prevents Spring from autowiring properties, constructor parameters, or method parameters if more than one bean matches the result. To illustrate the ambiguity of autowiring, suppose we annotate the setDessert() method with the @Autowired annotation:

 

In this example, Dessert is an interface, and there are three classes that implement this interface,
Cake, Cookies, and IceCream:

Because all three implementations are annotated with @Component, when components are scanned, they can be discovered and created as beans in the Spring application context. Then, when Spring tries to autowire the Dessert parameter in setDessert(), it doesn't have a unique, unambiguous optional value. While most people wouldn't have a hard time choosing from a variety of desserts, Spring couldn't. Spring has no choice but to declare failure and throw an exception. More precisely, Spring throws

NoUniqueBeanDefinitionException

  Of course, using the dessert example to illustrate the problems somewhat far-fetched. In practice, problems with autowiring ambiguity are rarer than you might think. Even if this ambiguity is indeed a problem, it is more common for a given type to have only one implementation class, so autowiring works well. However, when ambiguity does occur, Spring provides a variety of alternatives to resolve such issues. You can set one of the optional beans as the primary bean, or use a qualifier to help Spring narrow down the optional beans to just one bean.

1.1 Indicate the preferred bean

If you are like me and love all types of desserts like cakes, cookies, ice cream... they are delicious. But if there is only one dessert to choose from, which one is your favorite   ? Let's say ice cream is your favorite dessert. In Spring, a favorite scheme can be expressed via @Primary. @Primary can be used in combination with @Component on beans scanned by components, and in bean declarations in Java configuration in combination with @Bean. For example , the following code shows how to declare as the preferred bean:

The same can be done if you use XML to configure the bean. The <bean> element has a primary attribute to specify the preferred bean:

1.2 Defining autowired beans

  The @Qualifier annotation is the primary way to use qualifiers. It can be used in conjunction with @Autowired and @Inject to specify which bean to inject when injecting . For example, we want to make sure that we inject IceCream into setDessert()

middle:

  This is the simplest example of using qualifiers. The parameter set for the @Qualifier annotation is the ID of the bean you want to inject. All classes declared with the @Component annotation are created as beans, and the bean ID is the class name with the first letter changed to lowercase. Therefore , @Qualifier("iceCream") refers to the bean created by the component scan, and this bean is an instance of the IceCream class.

Pass

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325099971&siteId=291194637