Spring Bean Naming Conventions

In this short article, we will discuss what is standard naming conventions to the Spring beans in an Application Context in spring based applications.

Basically, in Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.

Every bean has one or more identifiers. These identifiers must be unique within the container that hosts the bean. A bean usually has only one identifier, but if it requires more than one, the extra ones can be considered aliases.

Bean Naming Conventions

The convention is to use the standard Java convention for instance field names when naming beans. That is, bean names start with a lowercase letter, and are camel-cased from then on.
Examples of such names would be (without quotes) 'accountManager', 'accountService', 'userDao', 'loginController', and so forth.
Naming beans consistently make your configuration easier to read and understand, and if you are using Spring AOP it helps a lot when applying advice to a set of beans related by name.
Let's look at spring bean naming conventions in different spring based configurations.

Spring Bean Naming Conventions in XML-based config

Spring Bean Naming Conventions in Java-based config

Spring Bean Naming Conventions in an annotation-based config

Related Spring Bean Scopes Posts

1. Guide to Spring Bean Scopes
This guide provides you what are different Spring bean scopes with examples.

2. Singleton and Prototype Bean Scopes Examples
In this article, we will discuss what is a difference between Singleton and Prototype scope with a source code example.

3. Spring InitializingBean and DisposableBean Example
This post guides you how to interact with the container’s management of the bean lifecycle by implementing Spring InitializingBean and DisposableBean interfaces.

4. Spring @Bean’s initMethod and destroyMethod Attributes Example
In this article, we will discuss how to use initMethod and destroyMethod attributes of the @Bean annotation to perform certain actions after bean initialization or before bean destruction by a container.

5. Spring @Scope annotation with Prototype Scope Example
In this article, we will discuss how to create a bean, scoped as a prototype, using the @Scope annotation.

Spring @Scope annotation with Singleton Scope Example
In this article, we will discuss how to create a bean, scoped as a singleton, using the @Scope annotation.

Comments