Spring Automatic annotation assembly, auto-detect assembly Bean mate Filter component use

Source: Internet
Author: User

Automatic annotation Assembly

When we assemble with annotations, we need to configure them in the XML file:

<context:annotation-config/> then we can use annotation-based automated assembly

Spring supports several different auto-annotation assemblies

[email protected] : Spring will try to use the Bytype automatic assembly method

For example: @autowired

Public People (Language Language) {

this. language =language;

Language.say ();

}

@autowired

public SetLanguage (languagelanguage) {

this. language =language;

Language.say ();

}

Generally for the bean containing the constructor, directly to the constructor annotation, if using setter assembly, directly to the attribute annotation.

You can also annotate attributes directly, so you should delete the setter method at this point.

In @autowired annotations, only one bean can be assembled into the properties of the associated bean, and if there are multiple beans or there are no paired beans, an exception is thrown. In spring, the solutions are presented separately:

Optional Auto-assembly

@Autowired indicates that the annotated property must not be empty, but using its Required property can specify @Autowired (required=false) to indicate that the annotated property can be empty

When the constructor is annotated with the same method, only one of the constructor's annotations required to true, and the others are false. For the default @autowired, however, the constructors with the most parameters are automatically assembled.

The dependence of the ambiguous limit

When multiple beans meet the requirements, you can combine @qualifier annotations to eliminate ambiguity.

For example:@autowired

@Qualifier ("中文版")

public SetLanguage (languagelanguage) {

this. language =language;

Language.say ();

}

Then in the Assembly, will choose the ID of the Chinese bean for assembly, at this time from the Bytype to byname automatic assembly, the scope of the assembly is reduced, has reached the purpose of eliminating ambiguity.

We can also annotate in the Chinese class: @Qualifier ("stringed")

or configure <bean id= "language" class= "中文版" in XML >

<qualifier value= "stringed"/></bean> can reach the same effect.

[Email protected]

@Inject injection As with @autowired injection, it is also possible to annotate properties, methods, constructors, unlike @autowired, where the @inject does not have a required attribute, so the @inject annotation dependency must exist or an exception will be thrown.

@Autowired annotations will be ambiguous, the same @inject will be ambiguous, and @autowired collocation @qualifier, @Inject with @named use can eliminate ambiguity, its role, usage and @qualifire, Specify an ID name.

Automatically detect beans

We use <context:component-scan/> to replace <context:annotation-config/>,<context:component-scan/> Not only can <context:annotation-config/> function, that is to eliminate the property and Constructor-arg configuration, it can also automatically detect the box assembly bean, that is, we can not use the bean element.

Using this method requires configuration in the XML file:

<context:component-scan base-package= "Com.springinaction.springidol"/>

The program automatically scans the Base-package and finds out which beans it labels.

To enable our program to automatically detect beans, what we need to do is to label a bean so that it can be automatically detected, and most commonly, @componenet annotations a bean, such as labeling a people class as a spring bean

@Component

Pubic class people{.......}//The Bean's ID is people

We can also specify the ID

@Component ("P")//the ID is P

The process is: when the program runs, by scanning the Base-package package, the class labeled by @componenet will register it as a spring bean and then exercise the <context:annotation-config/> function. The bean is automatically assembled.

Filter Component Scan

While using automatic detection of beans, we can also define our scanning strategy by filtering components, by configuring <context:include-filter/> and for <context:component-config/> <context:exclude-filter/> to achieve. The two filter components work just the opposite,<context:include-filter/> tell which classes need to be registered,<context:exclude-filter/> tell which classes do not need to be registered.

For example:

<context:component-scan>

<context:include-filter type= "assigable" expression= "Com.springinaction.springidol.Instrument"/>

</context:component-scan>

The combination of type and expression enables a class that inherits instrument to register as a spring bean.

Value of type:


Spring Automatic annotation assembly, auto-detect assembly Bean mate Filter component use

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.