5. Spring Beans Autowiring & Annotation Based Configuration(Autowiring)
Autowiring
- Auto-wiring, also known as automatic wiring, is a feature of the Spring Framework that automatically wires beans together by matching the types of the properties, constructor arguments, or method arguments of the beans.
- Spring Autowiring is a feature provided by the Spring IoC (Inversion of Control) container that allows for automatic resolution of dependencies between beans.
- Instead of explicitly configuring the relationships between beans in the XML configuration file using `<constructor-arg>` and `<property>` elements, autowiring allows the Spring container to automatically inject the required dependencies.
- Autowiring in Spring Beans helps in reducing the efforts of writing properties.
- If autowiring is enabled then spring container will take care about injecting the dependencies, and no need to configure into an xml file explicitly.
- Autowiring can't be used to inject primitive and string values, it will only supported if the dependancies are in the form of objects.
- All the Spring Beans autowiring should be specified at autowire attribute inside element <bean>, </bean>.
- To enable Autowiring, just define the "autowire" attribute in <bean>.
<bean id="employee" class="com.jwt.spring.Employee" autowire="byName"/>
Here are the different autowiring modes in Spring:
1. no - It is the default autowiring mode. It means no autowiring by default.
2. byName- The byName mode injects the object dependency according to name of the bean. In such case, property name and bean name must be same. It internally calls setter method. if the match is found it will inject those beans else it won’t be wired.
3. byType-The byType mode injects the object dependency according to type. So property name and bean name can be different. It internally calls setter method.
4. constructor- This is similar to byType but it applies to the arguments of the constructor. The constructor mode injects the dependency by calling the constructor of the class. It calls the constructor having large number of parameters.
5. autodetect- It is deprecated since Spring 3.
Example of autowiring by type:
<bean id="car" class="com.example.Car" autowire="byType"/>
In this example, if there is a single bean of any type that `Car` requires, Spring will automatically inject it. If there are multiple candidates or none, it will throw an exception unless the ambiguity is resolved.
Autowiring can simplify the configuration process, but it's essential to use it carefully, as it may lead to unexpected behavior in complex applications with multiple beans of the same type. It is crucial to understand the relationships between beans and how autowiring works to use it effectively.
Annotation Based Configuration
Starting from Spring 2.5 it became possible to configure the dependency injection using annotations. So instead of using XML to describe a bean wiring, you can move the bean configuration into the component class itself by using annotations on the relevant class, method, or field declaration.
1. @Required : The @Required annotation applies to bean property setter methods.
2. @Autowired : The @Autowired annotation can apply to bean property setter methods, non-setter methods, constructor and properties.
3. @Qualifier : The @Qualifier annotation along with @Autowired can be used to remove the confusion by specifiying which exact bean will be wired.There will be a situation where you will create more than one bean of the same type. But you will want to wire only one of them with the property. For those cases, you have to use @Qualifier annotation in Spring with @Autowired so as to remove the confusion by specifying the bean for wiring.
JSR-250 Annotations: (Java Specification Request) These Spring annotations are supported by Spring Framework including @Resource, @PreDestroy and @PostConstruct annotations. These annotations are not really required as you already have the alternatives.
Limitations of Spring Autowiring:
1. Ambiguity in Autowiring:
- If there are multiple beans of the same type that can be candidates for autowiring, Spring may face ambiguity in choosing the correct bean to inject.
2. Limited Support for Primitive Types:
- Autowiring is primarily designed for autowiring objects and may not work as effectively for autowiring primitive types.
- For autowiring primitive types, it is recommended to use @Value annotation or explicitly inject values through XML or JavaConfig.
Comments
Post a Comment