The Spring IoC container can help you to wire your beans automatically. You only have to specify the auto-wiring mode in the autowire attribute of <bean>. Table 3-2 lists the autowiring modes supported by Spring.
Mode | Description |
no* | No auto-wiring will be performed. You must wire the dependencies explicitly. |
byName | For each bean property, wire a bean whose name is the same as the property’s. |
byType | For each bean property, wire a bean whose type is compatible with the property’s. If more than one bean is found, an UnsatisfiedDependencyException will be thrown. |
constructor | For each argument of each constructor, first find a bean whose type is compatible with the argument’s. Then pick the constructor with the most matching arguments. In case of any ambiguity, an UnsatisfiedDependencyException will be thrown. |
autodetect | If a default constructor with no argument is found, the dependencies will be auto-wired by type. Otherwise, they will be auto-wired by constructor. |
* The default mode is no, but this can be changed by setting the default-autowire attribute of the <beans> root element. This default mode will be overridden by a bean’s own mode if specified.
Although the auto-wiring feature is very powerful, the cost is that it will reduce the readability of your bean configurations. As auto-wiring is performed by Spring at runtime, you cannot derive how your beans are wired from the bean configuration file. In practice, I recommend applying auto-wiring only in applications whose component dependencies are not complicated.
There are so many things that can go wrong by forcing wire, I will let spring wire for me.
Having said that, I will not be documenting this part with the exception of the below:
Wire the bean by name, @Resource(name = "datePrefixGenerator")
The bean named, datePrefixGenerator will be used.
package com.sequence; import javax.annotation.Resource; public class SequenceGenerator { @Resource(name = "datePrefixGenerator") private PrefixGenerator prefixGenerator; … }