Though autowiring in Spring makes life easy by wiring the dependencies automatically thus requiring less code to write but at the same time there are few limitations and disadvantages of autowiring in Spring too and that is the topic of this post.
Benefits of autowiring
- Using autowiring means you don't need to explicitly wire the bean properties, Spring will do it automatically. So autowiring requires less configuration code.
- Provides several modes for autowiring like byName, byType, constructor. So you have the flexibility to choose how you want to autowire the dependencies.
Disadvantages of autowiring in Spring
- Explicit wiring has an advantage that you can explicitly wire the bean you want but Autowiring is not that exact. For example, if you are using constructor autowiring and there is not exactly one bean of the constructor argument type in the container then Spring won't be able to determine which of those beans has to be wired. In case of ambiguity Spring autowiring won't try to guess instead it throws an exception.
- Wiring information may not be available to tools that may generate documentation from a Spring container.
- Within the container there may be multiple bean definitions matching the type specified by the setter method or constructor argument to be autowired. Within the container there may be multiple bean definitions matching the type specified by the setter method or constructor argument to be autowired. In that case Spring won't be able to decide which bean to use for autowiring. If no unique bean definition is available, an exception is thrown.
Limitations of autowiring
- Explicit dependencies in property and constructor-arg settings always override autowiring.
- You cannot autowire so-called simple properties such as primitives, Strings, and Classes (and arrays of such simple properties). This limitation in autowiring is by-design.
Reference- https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html
That's all for this topic Benefits, Disadvantages And Limitations of Autowiring in Spring. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Spring Tutorial Page
Related Topics
You may also like-
No comments:
Post a Comment