Java Reference
In-Depth Information
<bean id="sequenceGenerator"
class="com.apress.springenterpriserecipes.sequence.SequenceGenerator">
...
</bean>
However, XML has restrictions on the characters that can appear in the XML id attribute, but
usually you won't use those special characters in a bean name. Moreover, Spring allows you to specify
multiple names, separated by commas, for a bean in the name attribute. But you can't do so in the id
attribute because commas are not allowed there.
In fact, neither the bean name nor the bean ID is required for a bean. A bean that has no name
defined is called an anonymous bean . You will usually create beans like this that serve only to interact
with the Spring container itself; that you are sure you will only inject by type later on; or that you will
nest, inline, in the declaration of an outer bean.
Defining Bean Properties by Shortcut
Spring supports a shortcut for specifying the value of a simple type property. You can present a value
attribute in the <property> element instead of enclosing a <value> element inside.
<bean id="sequenceGenerator"
class="com.apress.springenterpriserecipes.sequence.SequenceGenerator">
<property name="prefix" value="30" />
<property name="suffix" value="A" />
<property name="initial" value="100000" />
</bean>
This shortcut also works for constructor arguments.
<bean name="sequenceGenerator"
class="com.apress.springenterpriserecipes.sequence.SequenceGenerator">
<constructor-arg value="30" />
<constructor-arg value="A" />
<constructor-arg value="100000" />
</bean>
Spring 2.0 provides another convenient shortcut for you to define properties. It's by using the p
schema to define bean properties as attributes of the <bean> element. This can shorten the lines of XML
configuration.
<beans xmlns=" http://www.springframework.org/schema/beans"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
xmlns:p=" http://www.springframework.org/schema/p"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
Search WWH ::




Custom Search