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 are the injection methods of Java spring?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you what Java spring injection methods, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

Set the scope of Spring

Or use enumerated values to set the

Singleton and Dolly usage scenarios

Automatic injection

@ Primary

Are there multiple implementations of an interface managed by spring? in dependency injection, spring will not know which implementation class to inject will throw a NoUniqueBeanDefinitionException exception.

Use @ Primary to tell Spring which implementation class to inject

Qualifier

The impact of @ ComponentScan configuration on performance

Performance impact of optimizing bean configuration on spring applications

In Spring, we can simplify the injection process of javaBean by setting the scanning range of the component.

Use the @ ComponentScan annotation to specify the package scanning division in scenarios where large-scale object injection is required. Of course, the classes in the specified package path need to be annotated with @ Component to allow Spring to create and manage.

Lazy loading

Spring uses preloading by default and creates a bean when the Spring project starts

Lazy loading: do not create a bean when the spring project starts, but when it is in use

Three injection modes

Field injection (IDEA prompts you not to recommend it)

Disadvantages of field injection:

The bean class injected by the field is not visible outside

Circular dependence problem

Cannot set the objects that need to be injected to final, nor can it inject immutable objects

The bean class injected by the field is not visible outside

The biggest problem with using field injection is the external visibility of the injection class (HealthRecorSercice), and the HealthRecorSercice can only be accessed in the ClientService class, and the separated container environment cannot be accessed, resulting in too high coupling between the class and the container to use the target object without the container.

A ClientService call to a method that contains field injection bean in the external class new throws a HealthRecorSercice null pointer exception

Circular dependence problem

The following An and B have circular dependencies, but the species that is legal in Spring does not throw an exception when the compiler starts the Sring class, but only reports an error when An or B is used

The object that is injected into the field is instantiated before the instantiation of another class is needed.

Constructor injection (official recommendation)

Constructor injection is through the constructor injection of the class.

Constructor injection can solve the problem:

Class is not visible outside

Cannot set the objects that need to be injected to final, nor can it inject immutable objects

Can't solve: circular dependency problem

The code is not readable.

Is there a circular dependency when using constructor injection? a cyclic dependency exception is thrown when the Spring project starts

Set method injection

Set injection solves the problem:

Class is not visible outside

Circular dependence problem

Cannot set the objects that need to be injected to final, nor can it inject immutable objects

The above is all the contents of the article "what are the ways to inject Java spring?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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

Development

Wechat

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

12
Report