Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse @ Autowired annotation shiyong

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to analyze the @ Autowired annotation shiyong. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Rules for the use of @ Autowired: there are candidate Bean of this type in the container

The container can contain multiple candidate Bean of this type

Prior to Spring 3.x, there can be only one Bean; in the Spring container with multiple Bean reporting exception BeanCreationException.

After Spring 3.x, you can have multiple Bean using @ Autowired with the same variable name as one of the multiple Bean of this type (that is, @ Autowired private Student student;,student above is the id of one of the multiple Bean). If rule 4 is violated, a BeanCreationException exception will be thrown.

As follows, we are using a version of Spring greater than 3.0. Entity class Student

Public class Student {private String name; / / getter and setter... } there are two bean we can write when using @ Autowired auto-assembly instance: @ Autowiredprivate Student student; or @ Autowiredprivate Student student02; because the two bean id of Student are student and student02, and our variables are also called student and student02, so they will not report an error. If the variable name stu does not have a corresponding bean id, an exception BeanCreationException will be thrown when used. If Spring uses a version earlier than 3.0, it is not allowed to have more than one Bean in a class, even if the variable name is the same as bean id. What if you have to have multiple bean?

At this point, you can use the @ Qualifier annotation in conjunction with the @ Qualifier annotation. The purpose of the @ Qualifier annotation is to help the Spring container find the bean. When used, the name attribute of @ Qualifier (name= "beanid") indicates which bean to use, so the Spring container knows which specific bean to inject. At this point, there can be such an existence in the Spring container: multiple bean in a class. The purpose of @ Qualifier is to disambiguate and indicate which bean to use. @ Autowired@Qualifier (name= "student02") private Student stu

At this point, we can also use @ Resource annotation to achieve the same function.

@ Resource (name= "student02") private Student stu; above is how to parse the @ Autowired annotation shiyong shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report