Annotation of Spring

Disclaimer: The materials used in this column are written by VIP students of Kaige Academy. Students have the right to remain anonymous and have the right to final interpretation of the article. Kaige Academy aims to promote VIP students to learn from each other based on public notes.

Annotation of Spring

Annotation provides a safe annotation-like mechanism, providing a formal way for us to add information to our code, so that we can conveniently use the data at a later point (by parsing the annotation to use this data) to associate any information or metadata with program elements (classes, methods, member variables, etc.). In fact, it is a more intuitive and clear description. The description information has nothing to do with the business logic of the program, and is used by the specified tool or framework. Annotation, like a modifier, is applied to the declarations of packages, types, constructors, methods, member variables, parameters, and local variables. Annotation is actually an interface. Annotation information is accessed through APIs related to Java's reflection mechanism. The relevant classes (classes in the framework or tools) use this information to decide how to use the program elements or change their behavior. The Java language interpreter ignores these annotations when working, so these annotations are "inactive" in the JVM, and the information of these Annotation types can only be accessed and processed through the supporting tools.

The traditional Spring approach is to use .xml files to inject beans or configure aops and things. This has two disadvantages:

1. If all the content is configured in the .xml file, then the .xml file will be very large; if the .xml file is separated according to requirements, then the .xml file will be very large. In short, this will lead to very low readability and maintainability of configuration files.

2. It is a troublesome thing to constantly switch between .java files and .xml files during development, and this incoherence of thinking will also reduce the efficiency of development.

In order to solve these two problems, Spring has introduced annotations. Through the "@XXX" method, the annotations are closely integrated with Java Beans, which not only greatly reduces the size of configuration files, but also increases the readability and cohesion of Java Beans. .

Several commonly used annotations

@Component represents a bean component, a description of the bean

@Resource Import dependent objects, property injection (no need to write names, the corresponding operation class will be found according to your property type, provided that the class you are looking for must also have @component managed by spring)

@Autowired has the same effect as @componentnt

@Scope specifies the scope of the bean

@Service usually works at the business layer, but currently the functionality is the same as @Component.

@Repository can only be annotated on DAO classes. The function of this annotation is not only to identify the class as a bean, but also to encapsulate the data access exception thrown in the marked class as Spring's data access exception type

@Constroller usually acts on the control layer, but currently the functionality is the same as @Component.

image

Spring IOC principle

1. Reflection mechanism (create objects through this)

2. Read XML using SAX to read

3. Annotation

image

image

image

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326228300&siteId=291194637