Home Tutorial Spring Spring3 Ioc Spring BeanNameAware


Share on Google+Share on Google+
Spring BeanNameAware
Posted on: September 6, 2010 at 12:00 AM
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) {
		this.name = name;

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


package com.roseindia.common;

import org.springframework.context.support.ClassPathXmlApplicationContext;
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="http://www.springframework.org/schema/beans"
  <bean id="studentBean" class="com.roseindia.common.StudentBean" />

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

org.springframework.context.support.ClassPathXmlApplicationContext@18a47e0: startup date [Mon Sep 06 10:55:50 GMT+05:30 2010]; root of context hierarchy
Bean name : satya

Download this example code


Related Tags for Spring BeanNameAware:

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.

Posted on: September 6, 2010

Recommend the tutorial

Advertisements Advertisements