Java Reference
In-Depth Information
<beans xmlns="
http://www.springframework.org/schema/beans"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
...
</beans>
Declaring Beans in the Bean Configuration File
Each bean should provide a unique
name
or
id
and a fully qualified class name for the Spring IoC
container to instantiate it. For each bean property of simple type (e.g.,
String
and other primitive types),
you can specify a
<value>
element for it. Spring will attempt to convert your value into the declaring type
of this property. To configure a property via setter injection, you use the
<property>
element and specify
the property name in its
name
attribute. A
<property>
requires that the bean contain a corresponding
setter method.
<bean name="sequenceGenerator"
class="com.apress.springenterpriserecipes.sequence.SequenceGenerator">
<property name="prefix">
<value>30</value>
</property>
<property name="suffix">
<value>A</value>
</property>
<property name="initial">
<value>100000</value>
</property>
</bean>
You can also configure bean properties via constructor injection by declaring them in the
<constructor-arg>
elements. There's not a
name
attribute in
<constructor-arg>
because constructor
arguments are position-based.
<bean name="sequenceGenerator"
class="com.apress.springenterpriserecipes.sequence.SequenceGenerator">
<constructor-arg>
<value>30</value>
</constructor-arg>
<constructor-arg>
<value>A</value>
</constructor-arg>
<constructor-arg>
<value>100000</value>
</constructor-arg>
</bean>
In the Spring IoC container, each bean's name should be unique, although duplicate names are
allowed for overriding bean declaration if more than one context is loaded. A bean's name can be
defined by the
name
attribute of the
<bean>
element. Actually, there's a preferred way of identifying a
bean: through the standard XML
id
attribute, whose purpose is to identify an element within an XML
document. In this way, if your text editor is XML-aware, it can help to validate each bean's uniqueness at
design time.
Search WWH ::
Custom Search