Spring Boot Autowiring
In Spring Boot, autowiring is a feature that allows Spring to automatically resolve and inject collaborating beans (dependencies) into your beans.
There are several types of autowiring available in Spring Boot:
Types of Autowiring
No Autowiring (
noordefault):- The default setting where no autowiring is performed. Dependencies need to be explicitly specified using
refattribute in the bean configuration.
- The default setting where no autowiring is performed. Dependencies need to be explicitly specified using
By Type (
autowire="byType"):- Spring container injects the dependency by matching the data type of the property to be autowired with the beans defined in the configuration file. If a bean of the matching type is found, it is injected. If there is more than one bean of that type, an error will be thrown unless a qualifier is specified.
By Name (
autowire="byName"):- Similar to byType, but the container looks for a bean with the same name as the property to be autowired. If a match is found, it injects the bean. If no matching bean is found, no injection occurs.
Constructor (
autowire="constructor"):- Autowires the dependencies by calling the constructor of the bean class with arguments that match the type of the properties. If the constructor has multiple parameters, it tries to match each parameter with the available beans in the configuration.
Autowire by @Autowired Annotation:
- The
@Autowiredannotation can be used on fields, setter methods, and constructors. It enables automatic dependency injection by type. It can be combined with the@Qualifierannotation to resolve conflicts when multiple beans of the same type are present.
- The
Autowire by @Resource Annotation:
- The
@Resourceannotation is part of JSR-250 and is used for dependency injection. It can be used to inject dependencies by name or by type
- The
Comments
Post a Comment