You are on page 1of 2

Basics - containers and beans

In Spring, those objects that form the backbone of your application and that are managed by the Spring IoC container are referred to as beans. A bean is simply an object that is instantiated, assembled and otherwise managed by a Spring IoC container; other than that, there is nothing special about a bean (it is in all other respects one of probably many objects in your application). These beans, and the dependencies between them, are reflected in the configuration metadata used by a container. Why... bean? The motivation for using the name 'bean', as opposed to 'component' or 'object' is rooted in the origins of the Spring Framework itself (it arose partly as a response to the complexity of Enterprise JavaBeans). Spring Framework (2.5.6) 32

3.2.1. The container
The org.springframework.beans.factory.BeanFactory is the actual representation of the Spring IoC container that is responsible for containing and otherwise managing the aforementioned beans. The BeanFactory interface is the central IoC container interface in Spring. Its responsibilities include instantiating or sourcing application objects, configuring such objects, and assembling the dependencies between these objects. There are a number of implementations of the BeanFactory interface that come supplied straight out-of-the-box with Spring. The most commonly used BeanFactory implementation is the XmlBeanFactory class. This implementation allows you to express the objects that compose your application, and the doubtless rich interdependencies between such objects, in terms of XML. The XmlBeanFactory takes this XML configuration metadata and uses it to create a fully configured system or application.

The Spring IoC container

). A bean will almost always have only one id. Adopting a consistent way of naming your beans will go a long way towards making your configuration easier to read and understand. However. Examples of such names would be (without quotes) 'accountManager'.). the XML specification does limit the characters which are legal in XML IDs. 'loginController'. This is usually not a constraint. adopting such naming standards is not hard to do. and are camel-cased from then on. 'accountService'. separated by a comma (. or names. or want to introduce other aliases to the bean. .6) 36 Bean naming conventions The convention (at least amongst the Spring development team) is to use the standard Java convention for instance field names when naming beans. the XML parser is able to do some extra validation when other elements reference the id. That is. as such. Please note that you are not required to supply a name for a bean. and if you are using Spring AOP it can pay off handsomely when it comes to applying advice to a set of beans related by name. and so forth.Naming beans The IoC container Spring Framework (2. If no name is supplied explicitly. 'userDao'. These ids must be unique within the container the bean is hosted in. The 'id' attribute allows you to specify exactly one id. but if a bean has more than one id. or whitespace in the 'name' attribute. you may also or instead specify one or more bean ids. semicolon (. and as it is a real XML element ID attribute. it is the preferred way to specify a bean id. bean names start with a lowercase letter. When using XML-based configuration metadata. the extra ones can essentially be considered aliases. but if you have a need to use one of these special XML characters.5. these terms refer to the same thing). Every bean has one or more ids (also called identifiers. the container will generate a unique name for that bean. you use the 'id' or 'name' attributes to specify the bean identifier(s). The motivations for not supplying a name for a bean will be discussed later.