Autowiring is the process of placing an instance of one bean into the specified field in an instance of another bean.
How Spring Autowiring Works?
All spring beans are managed by spring container called “application context”. The autowiring happens at the time of application starts up. When any autowiring configuration is found either by xml configuration meta data or @Autowired annotation, Spring will look for a class that matches the property in the applicationContext, and inject it automatically.
Note: There may be the situations when more than one property matched the class spring is looking for, in that case we have to provide the qualify information to help spring for identifying which one in should use.
Spring Autowiring Modes
- no: No autowiring at all. Bean references must be defined via a reference variable.
- byName: Autowiring by property name. This will inspect the application context and look for a bean named exactly the same as the property which needs to be autowired.
- byType: Allows a property to be autowired if there is exactly one bean of the property type in the application context. If there is more than one, a fatal exception is thrown.
- constructor: This is analogous to byType, but applies to constructor arguments.
- autodetect: chooses constructor or byType through introspection of the bean class. If a default constructor is found, the byType mode will be applied. It is deprecated since Spring 3.
- Spring autowire tutorial
- Spring autowire by name
- Spring autowire by type
- Spring autowire by constructor