Monday, August 24, 2020

Spring - AutoWiring

Spring automatically resolves the dependencies between the collaborating beans by inspecting the contents of the BeanFactory. This is called Autowiring.

Spring has 5 modes of autowiring:

1. no
    - default
    - means no autowiring
    - we have to use explicit bean reference for wiring

2. byName
    - autowiring by property name
    - The IoC container looks at the properties of the beans on which autowire attribute is set to byName in the XML configuration file

3. byType
    - autowiring by property datatype
    - The IoC container tries to match and wire a property if its type matches with exactly one of the beans name in configuration file.
    - if more than one such bean exists, a fatal exception is thrown

4. constructor
    - similar to byType but type applies to constructor arguments.

5. autodetect
    - spring first tries to autowire by constructor, if it fails, spring tries to autowire by type.

Limitations of autowiring

- overriding possibility 
    dependencies can still be specified using <constructor-arg> and <property> setting which will override autowiring
- primitive datatypes
    we cannot autowire primitive datatypes
- less exact
    autowiring is less exact than explicit wiring

No comments:

Post a Comment

Spring - AutoWiring

Spring automatically resolves the dependencies between the collaborating beans by inspecting the contents of the BeanFactory. This is called...