Home  >  Article  >  Java  >  What are the injection methods of Java spring?

What are the injection methods of Java spring?

WBOY
WBOYforward
2023-05-03 12:04:161162browse

Set the scope of Spring

Java spring的注入方式有哪些


Or use the enumeration value to set the

Java spring的注入方式有哪些

single case and Duoli usage scenarios

Java spring的注入方式有哪些

Automatic injection

Java spring的注入方式有哪些

@Primary

An interface has multiple implementations Is it managed by spring? In dependency injection, spring will throw NoUniqueBeanDefinitionException if it does not know which implementation class to inject.
Use @Primary to tell Spring which implementation class to inject

Java spring的注入方式有哪些

Qualifier

Java spring的注入方式有哪些

Java spring的注入方式有哪些

@ComponentScanThe impact of different configurations on performance

Optimizing bean configuration for spring applications Performance impact

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

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

Java spring的注入方式有哪些

Java spring的注入方式有哪些

Lazy loading

Java spring的注入方式有哪些


spring uses preloading by default. And create beans when the Spring project starts
Lazy loading: Do not create beans when the Spring project starts, create them when using

Java spring的注入方式有哪些

Java spring的注入方式有哪些

Three injection methods

Java spring的注入方式有哪些

Java spring的注入方式有哪些

Java spring的注入方式有哪些

#Field injection (IDEA will prompt that it is not recommended)

Disadvantages of field injection:

  • The bean class injected by the field is not visible externally

  • Circular dependency problem

  • Cannot set the object to be injected as final, nor can it inject those immutable objects


Java spring的注入方式有哪些

Java spring的注入方式有哪些##The bean class injected by the field is not visible externally

Java spring的注入方式有哪些#The biggest problem with using field injection is the external visibility of the injected class (HealthRecorSercice), and HealthRecorSercice can only When accessed in the ClientService class, the detached container environment cannot be accessed, which results in the coupling between the class and the container being too high and the target object cannot be used without the container.

In the external class new, a ClientService calls a method that contains a field injection bean, and a HealthRecorSercice class null pointer exception will be thrown.


Circular dependency problem

As follows, A and B have a circular dependency. , but it is legal in Spring. No exception will be thrown when compiling and starting the Sring class. An error will only be reported when A or B is used.

Java spring的注入方式有哪些 is fielded The injected object is instantiated before the instantiation of other classes that need to be injected.

Constructor injection (official recommendation)

Constructor injection is injected through the constructor of the class

Constructor injection can solve:


    Invisible outside the class
  • Cannot set the object to be injected as final, nor can it inject those immutable objects
  • Cannot solve: circular dependency Problem
The code is not very readable


Is there a circular dependency when using constructor injection? A circular dependency exception will be thrown when the Spring project is started.

Java spring的注入方式有哪些

set method injection

set Injection can solve:

  • Invisible outside the class

  • Circular dependency problem

  • Unable to set the required The injected object is final, and those immutable objects cannot be injected

Java spring的注入方式有哪些

#

The above is the detailed content of What are the injection methods of Java spring?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete