Spring XML configuration-use annotation assembly (@Atutowired, @Inject, @Resource)

Source: Internet
Author: User

Chen Kozhan--http://blog.csdn.net/u013474104/article/details/44352765

=======

1. Assembly Terminology

The behavior of creating collaborative relationships between application objects is often referred to as Assembly

2. Assembling with annotations

Spring is the introduction of automatic assembly using annotations starting from Spring2.5.

The spring container is disabled by default for annotation assembly, so if you want to use spring's annotation assembly, you must enable it. Enabled: Using the <context:annotation-config> element in the spring context namespace configuration, the configuration is enabled as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans        http://www.springframework.org/schema/ Beans/spring-beans-3.0.xsd        http://www.springframework.org/schema/context/         http Www.springframework.org/schema/context/spring-context-3.0.xsd "><!--spring default is to disable annotation assembly, Add the following line code to enable spring support for several different annotations for automatic assembly spring comes with the @autowiredjsr-330 of the @injectjsr-250 @resource--><context: Annotation-config/></beans>

Once the configuration is complete, we can annotate the code to identify that spring should automatically assemble the properties, methods, and constructors

Well, we can see that in the automatic assembly configuration of the above configuration, spring supports several different annotations for automatic assembly, and then I'll go through the automated assembly using each of the different annotations.

3.4 Types of automatic assembly

Spring has a number of ways to handle when it comes to dependencies on automatically assembling beans. As a result, Spring offers 4 unique automated assembly strategies.

A.byname: automatically assembles other beans with the same name (or ID) of the Bean's property into the corresponding attribute of the bean. If not, the assembly is not carried out;

B.bytype: automatically assembles other beans of the same type as the bean's attributes into the corresponding properties of the bean. If not, the assembly is not carried out;

c.constructor: other beans with the same type as the constructor of the bean are automatically assembled into the corresponding entry parameters of the Bean constructor;

d.autodetect: first try constructor for automatic assembly. If it fails, then try to use Bytype for automatic assembly;

4.Spring comes with a @autowired

First, @Autowired can make a variety of assembly methods for the Bean's properties, such as the setter method of the property, any method that the bean references, the constructor, and the ability to use @autowired to directly label attributes and remove setter methods, such as:

Note: When performing @autowired annotations, spring will attempt to perform Bytype automatic assembly of the method

The Helloword object has an object with a Hellochina property, and we want to annotate the Hellochina assembly!

1.setter method @autowiredpublic void Sethellochina (Hellochina hellochina) {This.hellochina=hellochina;} 2. Any method @autowiredpublic void xx (Hellochina hellochina) {This.hellochina=hellochina;} 3. Constructor @autowiredpublic Helloword (Hellochina hellochina) {This.hellochina=hellochina;} 4. Delete Setter method @autowiredprivate Hellochina Hellochina;

look, @Autowired won't even be limited to the private keyword, is that @autowired annotations without any limitations?

That's not true, @Autowired annotations do exist in two scenarios that hinder our work. Specifically, there must be only one bean suitable for assembly into the attribute or parameter identified by the @autowired annotation. If there are no matching beans, or if there are multiple matching beans, @Autowired annotations will encounter some trouble (exception: Nosuchbeandefinitionexception).

The solution for this type of trouble is as follows:

A. Optional automatic assembly

The property does not have to be assembled, and a null value can be accepted:

@Autowired (required=false) private Hellochina Hellochina;
Indicates that if Hellochina is not found for assembly, then Hellochina is null;

Note: The Required property can be used anywhere the @autowired annotation is used. When used for constructor assembly, however, only one constructor can set the required property of @autowired to True. Other constructors identified by using the @autowired annotation can only set the required property to False. In addition, when you use @autowired to identify multiple constructors, spring selects the one with the most parameters in the constructor that satisfies the assembly condition;

B. Limitations on ambiguity

When a bean's properties have multiple beans suitable for automatic assembly, then @autowired should use that bean? Then @autowired did not know.

In order to make sure that we need to assemble the bean, we can use @qualifier to explicitly specify the bean named Xxname.

@Autowired @qualifier ("Xxname") private Hellochina Hellochina;
As shown above, @Qualifier annotations attempt to inject a bean with ID xxname.
Principle, which is to convert Bytype automatic assembly to byname automatic assembly.

@Qualifier qualifier, but also can be created from, that is, to meet our needs, this is not introduced, the need for Google to go;

5.jsr-330 's @inject

to unify a variety of programming models that rely on the injection framework, JCP recently released the Java Dependency Injection specification, which JCP called JSR-330 . @Inject annotations are the core parts of JSR-330. This annotation can almost completely replace spring's @autowired annotations. So in addition to using spring-specific @autowired annotations, we can choose @Inject.

And @autowired one, @Inject can be used to automatically assemble properties, methods, and constructors; unlike @autowired, @Inject have no required properties. Therefore, the dependency of the @inject annotation injection must exist, otherwise the exception will be reported.

As with @autowired, @Inject has its own method of limiting ambiguity, which is configured as follows:

@Autowired @named ("Xxname") private Hellochina Hellochina;
In addition, @Inject can also create their own qualifier, here will not rip ....

Note: In fact, the @Named annotation is a note annotated with the @qualifier annotation
6. Inject an expression into the annotation:

SPRING3.0 introduced @value, which allows us to use annotations to assemble string-type values and base-type values, such as: int, Boolean.

@Value ("Chen Kozhan") Private String myName;
The above is purely hard coded, and you may ask what is the point of setting a value to death.

@Value +spel expression to show the powerful magic, here is not explained, interested in going to Google it ....


Spring XML configuration-use annotation assembly (@Atutowired, @Inject, @Resource)

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.