Quickly understand various annotations in spring
Annotations in spring can be roughly divided into two categories:
1) Spring's bean container related annotations, or bean factory related annotations;
2) Spring MVC related annotations.
Annotations related to spring's bean container include: @ required, @ Autowired, @ postconstruct, @ predestory, and spring 3 0 started to support the jsr-330 standard javax inject.* Comments in (@ inject, @ named, @ qualifier, @ provider, @ scope, @ singleton)
Annotations related to spring MVC include: @ controller, @ requestmapping, @ requestparam, @ ResponseBody, etc.
To understand annotations in spring, you must first understand annotations in Java.
1. Annotations in Java
Annotations are introduced in Java 1.5. We should be most familiar with @ override, which is defined as follows:
From the annotation, we can see that @ override is used to prompt the compiler that the method using @ override annotation must override the parent class or Java A method with the same name in lang.Object. We see that @ target and @ retention are used in the definition of @ override. They are the so-called "meta annotations" -- the annotations defining annotations, or the annotations of annotations (dizzy...). Let's take a look at @ retention
@Retention is used to prompt how long the annotation is retained. There are three values:
RetentionPolicy. Source remains at the source level and is discarded by the compiler (@ override is this type); RetentionPolicy. Class is kept at the compiled class file level by the compiler, but is discarded by the virtual machine;
RetentionPolicy. Runtime remains until runtime and can be read by reflection.
Look at @ target:
@Target is used to prompt where the annotation is used. The values are:
Respectively indicate where the annotation can be used: 1) class, interface, annotation, enum; 2) Attribute domain; 3) Methods; 4) Parameters; 5) Constructor; 6) Local variables; 7) Annotation type; 8) Package
So:
It means that @ override can only be used on methods. It is retained at the source level, processed by the compiler, and then discarded.
There is also a frequently used meta annotation @ documented:
Indicates whether the annotation can be processed by Javadoc and retained in the document.
2. Use meta annotations to customize annotations and handle custom annotations
With meta annotations, I can use it to customize the annotations we need. Combining custom annotations with AOP or filters is a powerful weapon. For example, you can use annotations to achieve fine-grained control of permissions -- use permission annotations on classes or methods, and then intercept them in AOP or filters. The following is the implementation of an annotation on login permissions:
We have customized an annotation @ nologin, which can be used on methods and classes. The annotation is kept until the runtime and can be read by reflection. The annotation means that the class or method annotated by @ nologin can be accessed even if the user is not logged in. The following is how to handle the annotation:
We defined a login interceptor above. First, we use reflection to judge whether the method is annotated by @ nologin:
NoLogin noLogin = handlerMethod. getmethod(). getAnnotation(NoLogin.class); Then judge whether the class is annotated by @ nologin:
noLogin = handlerMethod. getmethod(). getDeclaringClass(). getAnnotation(NoLogin.class); If it is annotated, it returns true. If it is not annotated, it determines whether it has logged in. If it has not logged in, it returns an error message to the foreground and false This is a simple example of permission processing using annotations and filters. Extended, we can use annotations to indicate that a method or class can only be accessed by users with certain roles or permissions, and then judge and process it in the filter.
3. Annotations related to spring bean container
1) @ Autowired is the annotation we use most. In fact, autowire = bytype is the automatic injection of dependencies according to types (annotation based dependency injection), which can be used on attribute fields, methods and constructors.
2) @ qualifier is autowire = byname. When the @ Autowired annotation judges that multiple beans have the same type, you need to use @ qualifier ("xxbean") to specify the ID of the dependent bean:
3) @ resource belongs to jsr250 standard and is used in attribute fields and methods. It is also a dependency injection of type byname. Usage: @ resource (name = "xxbean") The default @ resource class name without parameters is lowercase.
4) Jsr-330 standard javax inject.* Comments in (@ inject, @ singleton)@ Inject is equivalent to @ Autowired and @ named is equivalent to @ qualifier. In addition, @ named is used on the class and has the function of @ component.
5) @ component, @ controller, @ service, @ repository, these annotations are different from the above annotations. The above annotations are injected into the dependent beans, and these annotations are used to produce beans. These annotations are annotated on the class and annotate the class into beans in the spring bean factory one by one@ Controller and @ repository are basically @ components with more detailed semantics.
6) @ postconstruct and @ predestroy are not used for dependency injection, but for the life cycle of beans. Similar to init method (initializing bean) and destroy method (disposablebean)
4. Annotation processing in spring
Annotation processing in spring is basically carried out by implementing the interface beanpostprocessor:
Related processing classes include: autowiredannotationbeanpostprocessor, commonannotationbeanpostprocessor, persistenceannotationbeanpostprocessor, requiredannotationbeanpostprocessor
These processing classes can be implicitly configured into the spring container through < context: annotation config / >. These are the processing of dependency injection and the annotation (@ component, @ controller, @ repository) of production beans:
These are done by specifying the base package path for scanning and scanning them into the bean container of spring. Note that context: component scan will also configure autowiredannotationbeanpostprocessor and commonannotationbeanpostprocessor by default. Therefore, < context: annotation config / > can be omitted. In addition, context: component scan can also scan @ aspect style AOP annotations, but you need to add < AOP: AspectJ AutoProxy / > to the configuration file for cooperation.
5. Differences between spring annotation and jsr-330 standard annotation:
summary
The above is all about the quick understanding of various annotations in spring. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!