Beans are “lazily” loaded into bean factories, meaning that while the bean factory will imme-
diately load the bean definitions
A bean factory lazily loads all beans, deferring bean
creation until the getBean() method is called. An application context is a bit
smarter and preloads all singleton beans upon context startup. By preloading sin-
gleton beans, you ensure that they will be ready to use when needed—your appli-
cation won’t have to wait for them to be created.
Initialize beans.
If the bean implements InitializingBean, its
afterPropertiesSet() method will be called. If the bean
has a custom init method declared, the specified initialization
method will be called.
Notice that the value attribute is used exactly the same when setting a numeric
value as it is when setting a String value. Spring will determine the correct type
for the value based on the property’s type. Since the age property is an int,
Spring knows to convert 37 to an int value before calling setAge().
inner bean.
<bean id="kenny"
class="com.springinaction.springidol.Instrumentalist">
<property name="song" value="Jingle Bells" />
<property name="instrument">
<bean class="org.springinaction.springidol.Saxophone" />
</property>
</bean>
wiring collection
<list> Wiring a list of values, allowing duplicates.
<set> Wiring a set of values, ensuring no duplicates
<map> Wiring a collection of name-value pairs where name and value can be of any type
<props> Wiring a collection of name-value pairs where the name and value are both Strings
The key difference between the two is that when using
<props>, both the keys and values are Strings, while <map> allows keys and val-
ues of any type.
map 举例:
<map>
<entry key="GUITAR" value-ref="guitar" />
<entry key="CYMBAL" value-ref="cymbal" />
<entry key="HARMONICA" value-ref="harmonica" />
</map
<property name="someNonNullProperty"> <null/> </property>
Spring provides four flavors of autowiring:
■ byName—Attempts to find a bean in the container whose name (or ID) is
the same as the name of the property being wired. If a matching bean is not
found, the property will remain unwired.
■ byType—Attempts to find a single bean in the container whose type
matches the type of the property being wired. If no matching bean is found,
the property will not be wired. If more than one bean matches, an
org.springframework.beans.factory.UnsatisfiedDependencyExcep-
tion will be thrown.
■ constructor—Tries to match up one or more beans in the container with
the parameters of one of the constructors of the bean being wired. In the
event of ambiguous beans or ambiguous constructors, an org.springframe-
work.beans.factory.UnsatisfiedDependencyException will be thrown.
■ autodetect—Attempts to autowire by constructor first and then using
byType. Ambiguity is handled the same way as with constructor and
byType wiring.
转载于:https://blog.51cto.com/vcycyv/291252