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

Detailed explanation of @ Resource,@Autowired,@Inject3 injection methods in java

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

Share

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

This article mainly introduces "detailed explanation of @ Resource,@Autowired,@Inject3 injection methods in java". In daily operation, I believe many people have doubts about the detailed explanation of @ Resource,@Autowired,@Inject3 injection methods in java. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful for you to answer the doubts of @ Resource,@Autowired,@Inject3 injection methods in java! Next, please follow the editor to study!

General situation

The three types of @ Resource,@Autowired,@Inject are used to inject bean, and they belong to different programs.

ANNOTATIONPACKAGESOURCE@Resourcejavax.annotationJava JSR-250@Injectjavax.injectJava JSR-330@Autowiredorg.springframework.bean.factorySpring 2.5 +

JSR is an acronym for Java Specification Requests, which means Java specification proposal. Is a formal request to JCP (Java Community Process) to add a standardized technical specification. Anyone can submit a JSR to add new API and services to the Java platform. JSR has become an important standard in the Java world.

Difference

@ Resource

It has two key properties: name- name and type- type

1. If name,type is specified, find a bean with the appropriate name and type in the Spring container. If you cannot find it, an error will be reported.

2. If only name is specified, a bean with the same name as name is found in the Spring container. If it cannot be found, an error is reported.

3. If only type is specified, find a bean with the same type as type in the Spring container. If you cannot find or find more than one bean, an error will be reported.

4. If no parameter is specified, the default is to find the field name to assemble. If it cannot be found, it will be assembled by type. If it cannot be found, an error will be reported.

@ Autowired

Assemble by type by default. If you can't find or find more than one, an error will be reported.

If you want to assemble by name, you need to use it in conjunction with another Spring annotation, Qualifier ("name").

Requred=true must be assembled by default. If it can be empty, it can be set to false. If Spring4+ is combined with jdk8+, you can also use the same effect as Optional and false, such as

@ Autowired

Private Optional userService

@ Inject

Similar to @ Autowired, it can completely replace @ Autowired, but this has no required attribute and requires that bean must exist.

If you want to assemble by name, you need to use it in conjunction with another javax annotation N ("name").

At this point, the study on "detailed explanation of @ Resource,@Autowired,@Inject3 injection methods in java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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