5.6.4 @Autowired Annotations Since spring was born, The Autowire and Dependency-check properties are always present. When JDK 5.0 was officially released, Spring introduced the corresponding @required for the Dependency-check attribute. Similarly, it introduces a corresponding @autowired annotation for the Autowire attribute, which extracts the following definitions. Java Code Collection Code @retention (retentionpolicy.runtime) @Target ({elementtype.constructor, Elementtype.field, Elementtype.method}) Public @interface autowired {//must satisfy the dependency check Boolean required () default true;} @Autowired annotations can act on the builder, Properties, methods. The method here is not limited to the set value method, that is, setter method, the common methods can be applied to this annotation. For example, this annotation is applied to the testBean2 and TESTBEAN3 attributes of the TestBean1 class in the Autowiringdemo project, as follows. Java Code Collection Code @autowired (required=false) private TestBean2 testBean2; @Autowired private TESTBEAN3 testBean3; To enable this annotation, autowiredannotationbeanpostprocessor must be defined in an XML configuration file, as shown below, from Autowired.xml. At run time, testBean2 and testBean3 managed beans are automatically injected into the testBean1. Does the developer notice the @autowired exposed required attribute, which is an important attribute of the dependency checking task. By default, all properties and methods that apply the @autowired annotation must find the appropriate collaborator, otherwise the Di container throws an exception and the value of the required property can be changed to change the behavior. Java Code Collection Code The @autowired annotations applied to the Set value method are given below. Java Code Collection Code @autowired (required=false) public void setTestBean2 (TestBean2 testBean2) {this.testbean2 = testBean2;} @Auto Wired public void SetTestBean3 (TestBean3 testBean3) {this.testbean3 = testBean3;} The @autowired annotations applied to the builder are given below. Java Code Collection Code @autowired (required=false) public TestBean1 (TestBean2 tb2, TestBean3 tb3) {this.testbean2 = TB2; this.testbea N3 = TB3; The @autowired annotations that apply to common methods are given below. Java Code Collection Code @autowired public void Preparedbean (TestBean2 tb2, TestBean3 tb3) {this.testbean2 = TB2; this.testbean3 = tb3; In addition to being able to automatically inject ordinary managed beans, @Autowired annotations can inject special objects such as the various metadata objects in the Di container where the managed bean is located, such as the following. @Autowired inject the current di container. Java Code Collection Code @autowired private ApplicationContext AC; The trade-offs between @autowired's required attributes and @required annotations @Required are annotations that are specifically used for dependency checking, and the required properties of @autowired annotations can be used to perform dependency checking. Once the @required is applied to the target SetPoint method, the DI container must successfully invoke this set-valued method @Required The annotation considers the dependency condition to be satisfied, or an exception is thrown. In contrast, when we set the required property of the @autowired annotation to False, the exception is never thrown even if the appropriate collaborator is not found. 5.6.5 fine-grained control autowiring policy when multiple collaborators meet Autowire injection conditions, we can enable The primary attribute of the element to avoid throwing the exception. However, the primary attribute does not meet the complex requirements of an enterprise application, such as when multiple collaborators of the same type need to be injected into the same managed bean. An example of an XML configuration is given below. Java Code Collection Code Now you want to inject each of them into the properties given below. Obviously, enabling the primary property is not possible, because once it is enabled, TESTBEAN2A, TESTBEAN2B, and TESTBEAN2C are referenced to the same managed bean. Java Code Collection Code @autowired 2.private TestBean2 testbean2a; @Autowired private TestBean2 testbean2b; @Autowired private TestBean2 testbean2c; To do this, we need to enable Elements and @qualifier annotations. An example of an adjusted XML configuration is shown below. Java Code Collection Code Accordingly, the definition of the attribute needs to be adjusted accordingly, and the code example is given below. Each @qualifier annotation Specifies the managed bean to inject, such as "2a" to inject The Value property of "2a" is the target of the managed bean. Java Code Collection Code @autowired @Qualifier ("2a") private TestBean2 testbean2a; @Autowired @Qualifier ("2b") private TestBean2 testbean2b; @Autowired [email protected] ("2c") private TestBean2 testbean2c; @Qualifier annotations can act on properties, parameters, classes, other annotations, and so on, such as the use of parameter-level examples below. Java Code Collection code @autowired private void Preparedbean (@Qualifier ("2a") TestBean2 testbean2a, @Qualifier ("2b") TestBean2 TESTBEAN2B, @Qualifier ("2c") TestBean2 testbean2c) {this.testbean2a = testbean2a; this.testbean2b = testbean2b; THIS.TESTBEAN2C = testbean2c; Developers can also build more complex autowiring strategies based on @qualifier annotations, and an annotated example is given below. Java Code Collection Code @retention (retentionpolicy.runtime) @Target ({Elementtype.field, elementtype.parameter}) @Qualifier Public @interface Finequalifier {String keyfine () default ""; String valuefine () Default "";} As with @qualifier annotations, examples of @finequalifier applications are given below. Java Code Collection Code @autowired @FineQualifier (keyfine= "key2a", valuefine= "VALUE2A") private TestBean2 testbean2a; @Autowired @FineQualifier (keyfine= "key2b", valuefine= "value2b")Private TestBean2 testbean2b; @Autowired [email protected] (keyfine= "key2c", valuefine= "value2c") private TestBean2 testbean2c; To match the use of @finequalifier annotations, the XML configuration file needs to be adjusted accordingly, and the following is an example of an adjusted configuration. Java Code Collection Code Alternatively, the developer can also enable element, configuration example below. When you specify both in the same managed bean And element is used, the Di container takes precedence over the 。 Java Code Collection Code 10.11. When the @finequalifier annotation does not apply a class-level @qualifier annotation, the developer must register it with the following object. Java Code Collection Code test. Finequalifier
@Autowired Annotations (GO)