Share on Google+Share on Google+

Spring BeanNameAware

In this example you will see how to implement BeanNameAware in your bean class.

Spring BeanNameAware

The Interface BeanNameAware is implemented by beans that help to aware of their bean name in bean factory. The setBeanName method set the name for the bean in the bean factory. In this example you will see how to implement BeanNameAware in your bean class.

package com.roseindia.common;

import org.springframework.beans.factory.BeanNameAware;

class StudentBean implements BeanNameAware {
	private String name;

	public void setBeanName(String name) { = name;

	public void showBean() {
		System.out.println("Bean name : " +;

package com.roseindia.common;

import org.springframework.context.ApplicationContext;

public class AppMain {

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
		StudentBean bean = (StudentBean) context.getBean("studentBean");


<beans xmlns=""
  <bean id="studentBean" class="com.roseindia.common.StudentBean" />

When you run this application it will display message as shown below:

org[email protected]18a47e0: startup date [Mon Sep 06 10:55:50 GMT+05:30 2010]; root of context hierarchy
Bean name : satya

Download this example code



Posted on: September 6, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null