A, @Autowired
Org.springframework.beans.factory.annotation.Autowired
Public @interface autowired
Marks a constructor, field, setter method or Config method as to is autowired by Spring ' s dependency injection facilities.
Label a constructor, field, setter method, or configuration method to automatically reload it through spring's dependency injection method.
Only one constructor (in max) of any given Bean class could carry this annotation, indicating the constructor to Autowire WH En used as a Spring bean. Such A constructor does not has a to is public.
There can be only one constructor for any given Bean class to carry this annotation, indicating that the class will be used for automatic assembly when used as a spring bean. This constructor does not have to be public.
Fields is injected right after construction of a beans, before any config methods is invoked. Such a config field does not has to is public.
After a bean is created, the field is injected before any method is called. This field is not required to be public.
Config methods may have a arbitrary name and any number of arguments; Each of the those arguments is autowired with a matching beans in the Spring container. Bean Property setter Methods is effectively just a special case of such a general config method. Such config methods.
Configuration method It can be any name and have any number of arguments. Any one of the parameters of this method will be loaded using a matching bean from the spring container. The setter method of the bean attribute is also valid, which is a special case of the common configuration method. The configuration method does not have to be public.
In the case of multiple argument methods, the ' required ' parameter are applicable for all arguments.
When a method with more than one parameter is in the case, ' required ' will apply to all parameters.
In case of a Collection or Map dependency type, the container would autowire all beans matching the declared value type. In case of a Map, the keys must is declared as type String and would be resolved to the corresponding bean names.
Collection or map dependency type case, the container will automatically assemble the bean matching declared value type, the map key must be STRING,MAP values must be a known type.
Property
Boolean Required
Declares whether the annotated dependency is required.
Defaults to True.
Declares whether this annotation depends on the need, which is true by default.
A.1, Array, Set, Map
When we annotate arrays, sets, and maps with @Autowired, thecontainer automatically binds all matching beans in the container . We have modified the example in the previous section to illustrate the problem.
Example 1
1.Foo class
Use automatic binding for an array
Java code
- Public class Foo {
- @Autowired
- private bar[] bars;
- }
2. Configuration Files
There are 2 bars in the configuration file , so the two beans(objects) are automatically assembled into foo .
XML code
- <beans>
- <context:annotation-config/>
- <Bean id="foo" class="X.y.foo" />
- <Bean id= "bar1" class="X.y.bar">
- <property name="a" value="bar1" />
- </Bean>
- <Bean id= "bar2" class="X.y.bar">
- <property name="a" value="bar2" />
- </Bean>
- </Beans>
3. Test class
The result is returned as 2 in the test class .
Java code
- Foo foo = (foo) ctx.getbean ("foo");
- Bar[] Bars=foo.getbars ();
- System.out.println (bars.length);
The set and map implementations are similar in that they are required for definition and must be defined in the following format:
Set<bar>
Map<string,bar>
A.2, required
In the above example, if there is no bar in the container, there will be an error, sometimes we allow the automatic assembly of the property is null, we need to define:
Java code
- @Autowired (required=false)
- Private bar Bar;
This will automatically assemble null if there is no bar in the container.
B, @Qualifier
Org.springframework.beans.factory.annotation.Qualifier
Public @interface Qualifier
This annotation is used on a field or parameter as a qualifier for candidate beans when autowiring. It may also is used to annotate, other custom annotations, can and turn be used as qualifiers.
@Qualifier used to annotate a field or parameter when it is automatically bound as a qualifier for a candidate bean. It can also be used for custom qualifier annotations.
Property
Value
B.1, illustrative examples
We use @autowired to automatically bind by type, and when there are multiple matching beans in the container , which one is bound? we can Specify the bean to bind by using the @Qualifier (value= "Bar1") on the field or parameter that needs to be bound. and use <bean id= "Bar1" in the bean definition or <qualifier value= "bar1"/> to associate.
Example 1
1.Foo class
Java code
- Public class Foo {
- @Autowired
- @Qualifier (value="bar1")
- Private bar Bar;
- }
2. Configuration Files
There are 2 bars in the configuration file , one bean using qualifier for labeling, and one using ID for labeling, the container will select the bean that matches the @Qualifier (value) to bind.
XML code
- <beans>
- <context:annotation-config/>
- <Bean id="foo" class="X.y.foo" />
- <Bean class="X.y.bar">
- <qualifier value="bar2"/>
- <property name="a" value="bar2" />
- </Bean>
- <Bean id= "bar1" class="X.y.bar">
- <property name="a" value="bar1" />
- </Bean>
- </Beans>
C, @Required
Org.springframework.beans.factory.annotation.Required
Public @interface Required
Marks a method (typically a JavaBean setter method) as being ' required ': That's, the setter method must be configured to is dependency-injected with a value.
Labeling a method (usually a JavaBean setter method) is @required, that is, the setter method must be defined as a dependency injection through a value.
Detailed @autowired, @Qualifier and @required