Auto
wiring is a technique, where spring automatically inject a bean by using type,
name (or) constructor arguments. Spring supports 4 modes of auto wiring.
1. No Auto wiring
2. Auto wiring by name
3. Auto wiring by type
4. Auto wiring by constructor.
In
this post, I am going to explain these in brief, later posts explain each mode
in detail with an example.
Mode
|
Description
|
no
|
This
is the default mode, it specifies no auto wiring. Bean references are
injected using ref element.
|
byName
|
Spring
checks for a bean with same name as the proeperty that needs to be autowired.
|
byType
|
Allows
a property to be autowired if exactly one bean of the property type exists in
the container. If more than one bean of same property exists, then a fatal
exception is thrown.
|
constructor
|
Analogous
to byType, but applies to constructor arguments. If there is not exactly one
bean of the constructor argument type in the container, a fatal error is
raised.
|
Note
a. Explicit dependencies
in property and constructor-arg settings always override autowiring.
How to exclude a bean
from autowiring?
In
Spring’s XML format, set the autowire-candidate attribute of the <bean/>
element to false; the container makes that specific bean definition unavailable
to the autowiring infrastructure.
You
can also limit autowire candidates based on pattern-matching against bean
names. The top-level <beans/> element accepts one or more patterns within
its default-autowire-candidates attribute. For example, to limit autowire
candidate status to any bean whose name ends with Repository, provide a value
of *Repository. To provide multiple patterns, define them in a comma-separated
list. An explicit value of true or false for a bean definitions
autowire-candidate attribute always takes precedence, and for such beans, the
pattern matching rules do not apply.
No comments:
Post a Comment