Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Abstract:

When we use @autowired annotation, it is very common to report the above, what is the reason, and how to solve it, then kangkang this article!!

No more words, just serve the hard food!!

Optional automatic assembly

By default, @AutoWired is strongly contracted, which means that the tagged properties or parameters must be assemblable. If no Bean can be assembled into the tagged properties or parameters, autoassembly will fail. Throw NoSuchBeanDefinitionException abnormalities,

There are two cases:

(1) : Attributes do not have to be assembled, null values can also be used

In this scenario, it is optional to configure autoassembly by setting @AutoWired’s required property to false, as follows:

@Autowired(required = false)
private Instrument instrument;
Copy the code

Here, Spring ginger tries to assemble the instrument property, but if no matching bean of type Insrument is found, the application does not have any problems, and the value of the instrument property is set to null.

Note that the required attribute can be used anywhere the @AutoWired annotation is used, but when assembling as if with a constructor, only one constructor can set the @AutoWired attribute to True, Other constructors annotated with this annotation can only set the required attribute to false, and when multiple constructors are annotated with @AutoWired, Spring selects the constructor with the most arguments from all the constructors that meet the assembly criteria

② Spring has no shortage of beans suitable for assembly

The problem with this scenario is that spring may have multiple beans (at least 2) that meet the assembly conditions and can be assembled into properties or parameters. In this case, a new annotation collocation is created to help Spring identify bushi and select the kind and beautiful wife (the Bean needed). That’s the @qualifier annotation.

For example, to ensure that Spring is playing a guitar for the confessions Balloon Bean’s prelude, even if there are other instruments bean can play the prelude, we can use the @qulifier annotation here to specify the use of the guitar (specifying the bean) :

@Autowired
@Qualifire("guitar")
private Instrument instrument;
Copy the code

The above injection will attempt to inject a Bean with the ID guitar;

Using @Qualifire means converting @AutoWired’s byType auto gear into a displayed byName assembly, whereas the example above narrowed down the auto assembly candidate Bean by specifying the Bean ID to just one;

In addition to narrowing the scope through the Bean ID, we can also narrow the scope by using qualifier directly on the Bean, for example, assuming the guitar Bean is declared using the following XML

<bean class = "com.springinaction.beanxml.Guitar">
<qualifier value = "stringed"></qualifier>
</bean> 
Copy the code

The element here defines the guitar as a Stringed. In addition to specifying the qualifier in the XML, you can also use the @qualifire annotation to mark the guitar class:

@Qualifier("Stringed")
public class Guitar implements Instrument{}Copy the code

Note: Relationship @Qualifier note, see you in the next article (Manual dog head)