A detailed introduction to Spring's common annotations (you can learn to understand)

Table of contents

1. Why use annotations?

2. What are annotations?

3. Preliminary preparations for using annotations in Spring

4. Detailed introduction of @Component annotation

 5. Detailed introduction to @Value annotation

 6. Detailed introduction of @Autowired annotation

7. Detailed introduction of @Resource annotation

 8. How to choose to create objects and assign values ​​based on xml or annotations?


1. Why use annotations?

Answer: When we first came into contact with Spring, all of us assigned values ​​to the objects to be created through the <bean> tag in the .xml file, so we would write a lot of <bean> tags, which seemed Very redundant. Therefore, we desperately need a simpler way to assign values ​​to objects. This situation has been solved by Spring developers, so there is the Spring annotation method we are going to introduce today.

We can use Spring to create objects, and we can also use annotations to assign values ​​to objects. Instead of simply using <bean>'s set injection, construction injection, and automatic injection to create objects and assign values.

Supplement: Regarding set injection and construction injection in Spring, you can refer to what I wrote earlier:  http://t.csdn.cn/inw4n

           Regarding automatic injection (byName, byTpye) in Spring, you can refer to   http://t.csdn.cn/T9Uhz I wrote earlier

2. What are annotations?

In fact, we are both familiar and unfamiliar with the term annotation. A proper understanding of what annotations are probably will allow us to at least not panic when we initially learn Spring annotations.

Officially start to introduce annotations: carefully look at the code we have learned before, and you will find that when we rewrite the toString method, there is an @override on the rewritten method. Then the one with the @ symbol is the annotation. If you look at it this way, the annotation is not very far away from us.

 Note: All annotations are of @interface type, you can put the mouse on the annotation and hold down the ctrl key at the same time, so that you can see the definition of this annotation.

 What are the attributes of this annotation written in the small red dot in the above figure. It's just that the @override annotation doesn't have its own attributes.

So: we can use annotations to create objects in Spring, and we can assign values ​​to objects while using annotations to create objects. The way to create objects using annotations in Spring is to create objects through reflection.

3. Preliminary preparations for using annotations in Spring

  • Spring-context dependencies and Spring-aop dependencies must be added to the Pom.xml file. Since the Spring-context dependency already includes the Spring-aop dependency, it is enough for us to add a Spring-context dependency at the end.
  • In Spring's configuration file applicationContext.xml, add a component component scanner, also called an annotation scanner.
<context:component-scan base-package="你使用注解的类所在的路径(写到包就行)"></context:component-scan>

4. Detailed introduction of @Component annotation (creating objects)

 5. Detailed introduction to the @Value annotation (assigning values ​​to properties of simple types)

 6. Detailed introduction of @Autowired annotation (for assigning values ​​​​to attributes of reference types )

7. Detailed introduction of @Resource annotation (assignment to attributes of reference type)

 8. How to choose to create objects and assign values ​​based on xml or annotations?

Answer: If you want to change the value of the created object frequently, then we use the xml-based method to create the object

           If the value of the created object is not changed frequently, then we use the host method to create the object and assign the value.

Supplement: I have tried, you can use the xml file to create objects, and then use annotations to assign values ​​to the created objects. ! !

Guess you like

Origin blog.csdn.net/weixin_44362089/article/details/127363134