In this example you will see how to implement BeanNameAware in your bean class.
In this example you will see how to implement BeanNameAware in your bean class.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"
|
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 |