Home >Java >javaTutorial >What are the injection methods of Java spring?
Or use the enumeration value to set the
single case and Duoli usage scenarios
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
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.
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
##The bean class injected by the field is not visible externally
#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
is fielded The injected object is instantiated before the instantiation of other classes that need to be injected.
Constructor injection (official recommendation)
Is there a circular dependency when using constructor injection? A circular dependency exception will be thrown when the Spring project is started.
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
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!