Home Tutorial Spring Spring3 Ioc Spring BeanNameAware

 
 

Spring BeanNameAware
Posted on: September 6, 2010 at 12:00 AM
Advertisement
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.

StudentBean.java

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);
	}
}

AppMain.java

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(
				"context.xml");
		StudentBean bean = (StudentBean) context.getBean("studentBean");
		System.out.println(context);
		bean.setBeanName("satya");
		bean.showBean();
	}
}

context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  <bean id="studentBean" class="com.roseindia.common.StudentBean" />
</beans>

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

Advertisement

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
 

 

 

DMCA.com