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

What is the difference between @ Resources, @ Inject and @ Autowired

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what's the difference between @ Resources, @ Inject and @ Autowired". In daily operation, I believe many people have doubts about the difference between @ Resources, @ Inject and @ Autowired. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "what is the difference between @ Resources, @ Inject and @ Autowired"! Next, please follow the editor to study!

@ Resources

The description of @ Resources in the official document:

The @ Resource annotation is part of the JSR-250 annotation collection and is packaged with Jakarta EE.

What is JSR-250? Visit this link: https://id=250https://id=330

Priority injected:

Match by Type

Match by Qualifier

Match by Name

Match by Type

Note that the highest priority for @ Inject injection is Match by Type, rather than @ Resource's Match by Name.

Arbitrarily define a Component to be injected:

Componentpublic class ArbitraryDependency {private final String label = "ArbitraryDependency"; public String toString () {return label;}}

Use @ Inject injection:

@ RunWith (SpringJUnit4ClassRunner.class) @ ContextConfiguration (loader=AnnotationConfigContextLoader.class, classes=ApplicationContextTestInjectType.class) public class FieldInjectIntegrationTest {@ Inject private ArbitraryDependency fieldInjectDependency; @ Test public void givenInjectAnnotation_WhenOnField_ThenValidDependency () {assertNotNull (fieldInjectDependency); assertEquals ("ArbitraryDependency", fieldInjectDependency.toString ());}} Match by Qualifier

Define a new component to be injected:

Public class AnotherArbitraryDependency extends ArbitraryDependency {private final String label = "AnotherArbitraryDependency"; public String toString () {return label;}}

Test the code:

@ RunWith (SpringJUnit4ClassRunner.class) @ ContextConfiguration (loader=AnnotationConfigContextLoader.class, classes=ApplicationContextTestInjectQualifier.class) public class FieldQualifierInjectIntegrationTest {@ Inject private ArbitraryDependency defaultDependency; @ Inject private ArbitraryDependency namedDependency; @ Test public void givenInjectQualifier_WhenOnField_ThenDefaultFileValid () {assertNotNull (defaultDependency); assertEquals ("ArbitraryDependency", defaultDependency.toString ());} @ Test public void givenInjectQualifier_WhenOnField_ThenNamedFileValid () {assertNotNull (defaultDependency) AssertEquals ("Another Arbitrary Dependency", namedDependency.toString ());}}

Same failure as the first attempt by @ Resource to inject through Match by Type, with exception: NoUniqueBeanDefinitionException

Avoid this exception with @ Qualifier:

@ Inject@Qualifier ("defaultFile") private ArbitraryDependency defaultDependency;@Inject@Qualifier ("namedFile") private ArbitraryDependency namedDependency;Match by Namepublic class YetAnotherArbitraryDependency extends ArbitraryDependency {private final String label = "YetAnotherArbitraryDependency"; public String toString () {return label;}}

Consumer Code:

@ RunWith (SpringJUnit4ClassRunner.class) @ ContextConfiguration (loader=AnnotationConfigContextLoader.class, classes=ApplicationContextTestInjectName.class) public class FieldByNameInjectIntegrationTest {@ Inject @ Named ("yetAnotherFieldInjectDependency") private ArbitraryDependency yetAnotherFieldInjectDependency; @ Test public void givenInjectQualifier_WhenSetOnField_ThenDependencyValid () {assertNotNull (yetAnotherFieldInjectDependency); assertEquals ("Yet Another ArbitraryDependency", yetAnotherFieldInjectDependency.toString ();}}

Application context Code:

@ Configurationpublic class ApplicationContextTestInjectName {@ Bean public ArbitraryDependency yetAnotherFieldInjectDependency () {ArbitraryDependency yetAnotherFieldInjectDependency = new YetAnotherArbitraryDependency (); return yetAnotherFieldInjectDependency;}} at this point, the study on "what's the difference between @ Resources, @ Inject and @ Autowired" is over. I hope I can 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

Development

Wechat

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

12
Report