Spring annotation (1) Overview
The core of Spring is dependency injection (DI), and the basis of dependency injection is the configuration of dependency information. These configurations are called metadata. In the previous study, we used xml-based configuration. These metadata configurations were registered in spring as BeanDefinition, and spring used this as a template to create beans and inject dependencies.
Starting from spring 2.0, spring has successively added support for annotation-type metadata configuration. Since then, we have another way to configure bean. Spring contains many annotations, such as @ AutoWired and @ Compoent, and some unfamiliar ones such as @ Inject @ Improt.
Category
Spring contains so many annotations that beginners are at a disadvantage. Therefore, Spring annotations can be classified. The core of Spring's xml Metadata configuration is bean definitions, attributes, and dependencies. Therefore, spring annotations can be divided into the following aspects:
This article first makes a simple look at these annotations, and then in the subsequent articles, give a more detailed description of the last three.
CATEGORY Overview
The following sections describe each type in detail and list the annotations supported by most Spring.
Container level
The container level refers to the annotation used to configure the Spring container, which corresponds to the SpringXML configuration file, as shown in
@ Configuration acts on the class, which can define many beans
@ Import introduce other configurations
@ ComponentScanAutomatically scans Bean component Configuration and works with Configuration
Bean level
The Bean level refers to the object (class or method) marked by the annotation. It registers a BeanDefinition with the Spring container and Spring creates a Bean instance.
@ Component it acts on a class with other annotations such as @ Service, and can define a bean
@ Named is similar to above, from JSR-330
Use @ Bean with @ Configuration or @ component
Bean attribute level
This category refers to attributes acting on beans, such as delayed loading and Scope,
@ Qualifier and @ autowired
@ Lazy delayed Loading
@ Scope defines bean Scope
@ Required can be marked on the Set method, indicating that this method needs to be executed. Otherwise, an exception occurs, and @ autowired can be used together.
@ PostConstruct and @ PreDestroy from JSR, acting on the lifecycle
Dependency level
Spring can use these annotations for dependency injection, which is usually automatic or with some auxiliary information.
@ Autowired: Automatic dependency injection. There are several methods, such as byType and byName, which are usually used with @ Qualifier.
@ Inject from the JSR-330, similar to the above, JSR-330 related Annotation
@ Resource is similar to the above, JSR-250 related Annotation
@ Value Original Type Injection
Summary
The above briefly illustrates the annotations supported by Spring and their basic functions, but they have been mentioned on paper so far. However, the general impression on Spring annotations is quite important. It should be noted that Spring annotations and XML configurations complement each other and have their respective advantages and disadvantages. They should be determined based on the actual situation in actual applications. The next article will detail the content described in this article.