3. In-depth Inversion of Control (IoC)

[Note]Draft

This is a first draft of this chapter. It is technically accurate, but not in the style or state of the final revision.

Now that you have a basic understanding of Spring IoC, we can go into more detail in areas already covered and some areas we didn't. We will cover different ways to configure beans with constructor and setter injection, collections, bean inheritance, and scope.

3.1 Bean Naming

Everyone follows some form of coding conventions as they are writing code. Many people and most open source projects follow Sun's Java Coding Conventions (http://java.sun.com/docs/codeconv) or some variation of them. It's also important to have a coding convention when naming beans. Spring suggests following the same coding convention as Sun's Java Coding Convention for naming variables. Which is basically clearly describing the function of each bean starting with a lower case first letter and then upper casing the first letter of each new word without using any spaces. Creating identifiers this way from compound words is also called camel case. Camel case is a reference to the “humps” in a variable name from the upper case letters. For example, a good name for a remote mail service would be remoteMailService. For an XSLT parser used to process an XML file for the mail body, a bean name could be mailBodyXsltParser.

In all the examples so far, the id attribute has been used to give a bean a unique identifier. Spring recommends using the id attribute because it is a real XML ID. This means that the id attribute must be unique and also the XML parser is able to perform some extra validation on the ID. Since the id attribute is a real XML ID only certain characters can be used for the ID. If there is a need to use a character that isn't allowed for the id attribute or to provide multiple identifiers for a bean, the name attribute can be used. A good example using the name attribute is when a bean name like '/personView.html' will be used to lookup which controller should handle a request in a web application. A forward slash isn't a valid character for an XML ID. When an id attribute isn't specified, Spring still needs a unique identifier for the bean and will generate a unique id for it.

The name attribute can be a single value or multiple values separate by a comma, semi-colon, or whitespace. This gives you the flexibility to define aliases for the same bean. Aliases could also be used if there was a possibility that multiple bean definitions using the same bean might one day each have their own bean definitions. In this case, an alias could be removed and a new bean definition created using the alias name as the id. There is also a a separate alias element. So aliases can be defined for a bean at some other point in the XML configuration file.

Example 3.1. Bean's Name Attribute and Aliasing

The following example shows three different values for the bean definition's name attribute. It also shows using the alias element to give even more aliases to the bean.

The first alias element gives an alias of messageElementRefLocal to the messageAttributeValue bean. The second alias element gives an alias of messageElementRefBean to the messageAttributeRef bean which is is the alias created by the first alias element. The third alias element gives an alias of messageElementInnerBean to the messageElementRefBean bean which is is the alias created by the second alias element.

Excerpt from chapter03-constructors-setters/src/main/resources/alias-applicationContext.xml
                
<bean name="messageAttributeValue,messageElementValue,messageAttributeRef"
      class="org.springbyexample.springindepth.bean.Message">
    <property name="message" value="Spring comes before summer." />
</bean>

<alias name="messageAttributeValue" alias="messageElementRefLocal" />
<alias name="messageAttributeRef" alias="messageElementRefBean" />
<alias name="messageElementRefBean" alias="messageElementInnerBean" />