Spring Bean names

Spring bean names are straightforward, except for cases where names are not explicitly specified. To start with, Spring bean names for an xml based bean definition is specified this way:

<bean name='sampleService1' class='mvcsample.beanname.SampleService'>
  <bean class='mvcsample.beanname.SampleDao'></bean>

For a Java @Configuration based bean definition, the method name of the @Bean annotated method becomes the bean name:

public static class SpringConfig{

 public SampleService sampleService(){
  return new SampleService(sampleDao());

 public SampleDao sampleDao(){
  return new SampleDao();


and for Stereotype annotation(@Component, @Service, @Repository etc) based beans, the value field indicates the bean name:

public class SampleDao {

public class SampleService {

Now, what happens for cases where the bean name is not specified.

XML Based Bean Configuration Case:

For an xml based configuration, a case where the bean name is typically not specified are for beans which can act on the entire bean factory – say for eg, to define a BeanPostProcessor or a BeanFactoryPostProcessor For eg. consider the following dummy BeanPostProcessor that just grabs all the bean names from bean factory:

public class BeanNameScanningBeanPostProcessor implements BeanPostProcessor{
 private List<String> beanNames = new ArrayList<>();
 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  return bean;

 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  return bean;

 public List<String> getBeanNames(){
  return this.beanNames;

It would typically be defined in an xml bean configuration this way:

<bean class='mvcsample.beanname.BeanNameScanningBeanPostProcessor'/>

A second case with xml based configuration where a name is typically not specified is with inner beans, for eg. defined this way:

<bean class='mvcsample.beanname.SampleService'>
  <bean class='mvcsample.beanname.SampleDao'></bean>

The bean names for these cases is handled by a component called the BeanNameGenerator. For the case of a top level bean the name typically ends up being the package qualified class name along with a count of the instances, this way:


For the case of an inner bean, since it exists only in the scope of its containing bean, the name is not that relevant, however internally it does get a name based on the hex hash code of the bean definition for eg, ‘mvcsample.beanname.SampleDao#1881ee8b’

Java Based @Configuration Case:

For java based @Configuration on the other hand it is not possible to specify a bean without a name, the bean name is the method name.

Annotation based Configuration

For stereotype annotation based bean, if the name is not explicitly specified with the value field of stereotype annotations, then the name is again generated by AnnotationBeanNameGenerator which is an implementation of the BeanNameGenerator strategy interface, the names generated is simply the short name of the class, for eg from the javadoc for AnnotationBeanNameGenerator – bean name for com.xyz.FooServiceImpl becomes fooServiceImpl.


So to finally conclude, if bean names are relevant to you in some way(for eg to disambiguate between multiple bean instances of same type), then it is best to be explicit about the names, otherwise depend on Spring to generate the bean names for you. In some cases, for eg. with Spring-data project it is possible to specify custom behavior for repositories as a separate bean and
Spring-data by default uses Spring naming conventions to find the custom implementation and understanding how bean names are generated helps.

Reference: Spring Bean names from our JCG partner Biju Kunjummen at the all and sundry blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

Leave a Reply

9 + six =

Java Code Geeks and all content copyright © 2010-2015, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below: