Major Benefits and Limits of Autowiring in Spring Java Web Development
In this post, you will learn about autowiring—its limitations, advantages, and disadvantages in detail. You also get to know about different modes of autowiring used in Java web development.
Join the DZone community and get the full member experience.
Join For FreeIn this post, you will learn about autowiring—its limitations, advantages, and disadvantages in detail. You also get to know about different modes of autowiring used in Java web development. The team of Java development experts will share the best examples to help you understand about these modes.
Advantages of Autowiring
- Autowiring requires less code because we don’t need to write the code to inject the dependency explicitly.
- It reduces develop time by removing the necessity of specifying properties and constructor arguments.
Disadvantages of Autowiring
- Autowiring is less exact than explicit wiring. Spring is careful to avoid guessing in the case of ambiguity that might have unexpected results, the relationships between Spring-managed objects are no longer document explicitly.
- Wiring information may not be available to tools that generate documentation from a Spring container.
- Multiple bean definition within the container may match the type specified by the constructor or setter method argument to be autowired.
Limitations With Autowiring
- Explicit dependencies in constructor-argument and property settings always override autowiring. You cannot autowire simple properties such as primitives, Strings, and Classes.
- Overriding possibilities: We can define dependencies using property or constructor-args tag which will always override autowiring.
- Primitive data type: We have to define primitive data types String or Integer using property or constructor-args tag. You cannot autowire these tags.
- Confusing Nature: If you have lot of dependency in a program, then it’s hard to find using autowire attribute of bean.
Autowiring
- Spring can autowire a relationship between collaborating beans without using constructor-arg and property tags which helps with the amount of XML configuration.
- You can allow Spring to resolve collaborators automatically for your bean by inspecting the contents of the Application Context.
- Autowiring of the Spring framework enables you to inject the object dependency implicitly. Autowiring needs significantly less specification with properties or constructor arguments.
- Autowiring can update configuration as your objects. For example, if you need to add a dependency to a class, that dependency can automatically satisfy your need to modify the configuration. Autowiring can be especially useful during development, without negation the option of switching to explicit wiring when the code becomes more stable.
Autowiring Modes
Modes | Description | |
1. | no | It’s a default autowiring mode. It means no autowiring. |
2. | byName | Autowiring using property name. Spring container looks the properties of the beans on which autowire attribute is set to byName in the XML configuration file. It tries to match and wire its properties with the beans defined by the same name in the configuration file. |
3. | byType | Autowiring using property type. Allows a property to be autowired if exactly one bean of property type exists in the container. If more than one exists, it’s a fatal exception is thrown, which indicates that you may not used byType autowiring for that bean. |
4. | constructor | Tries to match a constructor of the autowired bean with beans whose types are assignable to the constructor arguments. There is not exactly one bean of the constructor argument in the container, a fatal error is raise. |
5. | autodetect | Spring first tries to autowire by constructor, if it doesn't work then If that fails than tries to autowired by using byType. |
Example of Autowiring
public class Book
{
private Author author;
public Book(Author author) {
this. customer = customer;
}
public void setAuthor (Author author) {
this.author = author;
}
//.......
}
public class Author
{
//......
}
1. Auto-Wiring ‘no’
This is a default autowiring mode. It means no autowiring.
<bean id="book" class="com.spring.common.Book">
<property name="Java" ref="completereference"/>
</bean>
<bean id="author" class="com.spring.common.Author" />
2. Auto-Wiring ‘byName’
In the Spring framework, everything is given a name. Bean properties are given a name and the beans that are wired to those properties, too.
The name of a property happens to match the names of the bean that is to be wired to that property. Spring framework that the bean should be automatically wired into the property.
Autowired a bean by property name. In this case, since the name of the "author" bean is the same name of the “book” bean’s property (“Author”), Spring will be autowired to it via the setter method – “setAuthor (Author author)”.
<bean id="book" class="com.spring.common.Book" autowire="byName" />
<bean id="author" class="com.spring.common.Author" />
3. Auto-Wiring ‘byType’
- Autowiring using byType works similarly to byName. When attempting to autowire a property byType, Spring will look T beans whose type is assigned to the property’s type.
- Autowire a bean by data type. In this case, the data type of the “author” bean is the same as the data type of the “book” bean’s property (Author object), so, Spring will autowire it via the setter method – “setAuthor (Author author)“.
<bean id="book" class="com.spring.common.Book" autowire="byType" />
<bean id="author" class="com.spring.common.Author" />
4. Auto-Wiring ‘constructor’
- If your bean is configured using a constructor injection, you may choose to put the <constructor-arg> elements.constructor arguments from beans in the Spring context.
- Autowire a bean by property data type in constructor arguments. Since the data type of “author” bean is same as the constructor argument data type in “book” bean’s property (Author object), Spring autowired it using the constructor method – “public Book(Author author)“.
<bean id="vehicle" class="com.spring.common.Vehicle" autowire="constructor" />
<bean id="author" class="com.spring.common.Author" />
5. Auto-Wiring ‘autodetect’
- If you want to autowire beans, but you can’t decide which type of autowiring to use, have no fear. We can set the autowire attribute to autodetect.
- If a default constructor is found, it first tries to autowire by “constructor”; otherwise, if it doesn't work but fails, then it tries to autowire using “byType”. In this case, since there is a default constructor in the “Book” class, Spring autowired it via the constructor method – “public Book (Author author) “.
<bean id="vehicle" class="com.spring.common.Vehicle" autowire="autodetect" />
<bean id="author" class="com.spring.common.Author" />
You just learned about autowiring and how to use it in Java web development (Spring). Don’t forget to give credit to the Java development team that worked hard to help you understand the advantages, limitations, and disadvantages of autowiring.
Opinions expressed by DZone contributors are their own.
Comments