Detailed @autowired, @Qualifier and @required

Source: Internet
Author: User

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
    1. Public class Foo {
    2. @Autowired
    3. private bar[] bars;
    4. }

2. Configuration Files

There are 2 bars in the configuration file , so the two beans(objects) are automatically assembled into foo .

XML code
  1. <beans>
  2. <context:annotation-config/>
  3. <Bean id="foo" class="X.y.foo" />
  4. <Bean id= "bar1" class="X.y.bar">
  5. <property name="a" value="bar1" />
  6. </Bean>
  7. <Bean id= "bar2" class="X.y.bar">
  8. <property name="a" value="bar2" />
  9. </Bean>
  10. </Beans>

3. Test class

The result is returned as 2 in the test class .

Java code
    1. Foo foo = (foo) ctx.getbean ("foo");
    2. Bar[] Bars=foo.getbars ();
    3. 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
    1. @Autowired (required=false)
    2. 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
    1. Public class Foo {
    2. @Autowired
    3. @Qualifier (value="bar1")
    4. Private bar Bar;
    5. }

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
  1. <beans>
  2. <context:annotation-config/>
  3. <Bean id="foo" class="X.y.foo" />
  4. <Bean class="X.y.bar">
  5. <qualifier value="bar2"/>
  6. <property name="a" value="bar2" />
  7. </Bean>
  8. <Bean id= "bar1" class="X.y.bar">
  9. <property name="a" value="bar1" />
  10. </Bean>
  11. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.