Analysis of Spring annotation principles

Source: Internet
Author: User
How to use:

We are now working on a spring based annotation to simplify the configuration, Springframework 2.5 introduces a complete annotaion configuration annotation, where we can see @autowired and @resources annotations everywhere in our project,

You can look at this definition in Lbc-applicationcontext.xml.



Spring can also configure annotations through <context:annotation-config/>, and his role is to register autowiredannotationbeanpostprocessor with the Spring container, Commonannotationbeanpostprocessor,

Persistenceannotationbeanpostprocessor and Requiredannotationbeanpostprocessor these 4 beanpostprocessor. These containers can be used to identify the various annotations that we define in our code.

But if we define context:compontent-scan, we do not need to define <CONTEXT:ANNOTATION-CONFIG/>, because he has been included. The advantage of using scan is that we can include classes and packages that need to recognize annotations.

The official 2.5.6 document describes this:

After using Compontent-scan, we can define our own filter to include or exclude

We can then use spring to provide the

You can refer to the spring Official document Http://docs.spring.io/spring/docs/2.5.6/spring-reference.pdf with a detailed spring annotation document.

Principle Analysis

The SPRINGIOC container is divided into two processing strategies for class-level annotations and annotations within classes:

(1). Class-level annotations: such as @component, @Repository, @Controller, @Service, and JavaEE6 @managedbean and @named annotations are class-level annotations added above the class. The spring container scans the annotation bean definition class based on the filtering rules of the annotation and registers it with the spring IOC container.

(2). Annotations within the class, such as @autowire, @Value, @Resource, and EJB and webservice-related annotations, are internal annotations that are added to the fields or methods within the class, The SPRINGIOC container resolves annotations within the bean through the bean post annotation processor.

The Spring2.5.6 document provides several types of internal annotations for the above illustration, and different annotations have different methods of use, such as @autowired annotations can be on attributes, on setter methods, and on constructors, most of which are used in our engineering resources annotation, the difference between him and autowired is that autowired is the default according to type matching, if you need to assemble by name, you need to cooperate with @qualifier. Resources, by default, match by name and can specify its name.

The class of the spring scan annotation file is Classpathbeandefinitionscanner

The core classes that spring implements @autowire parsing and injection are implemented through Autowiredannotationbeanpostprocessor.

We can see through the list of its methods, where the injection of the field, the injection of the property, and the selection of the corresponding construction method to inject.

We combine the following selection of construction methods to see its implementation principle.


This is done mainly by the following steps

1, query the construction method from the cache of the construction method

2, if the cache does not exist, get all the construction methods based on reflection

3, traversing all construction methods, whether the query contains @autowired properties

4, to determine the autowired annotation specified in the required attribute (required attribute is to determine whether strong dependent) if there is a required use the default construction method.

5, returns the specified construction method

When injected, it is achieved through the inject method.

Summary

Spring simplifies our configuration by annotations, most of the work we are using in the project is based on annotations, the use of spring annotations, the official has very detailed documentation, spring's support for annotations are mainly through reflection to get the corresponding annotations, to do the appropriate processing, Most of our projects are used by @service and @autowired, but we can use other annotations to speed up our development and meet our diverse needs.

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.