Spring Inversion of Control (IoC) also known as
Dependency Injection (DI) is a process by which
objects define their dependencies with collaborating objects.
Spring provides BeanNameAware interface which allows us to have access
to name of the bean defined in bean definition.
BeanNameAware.java
package org.springframework.beans.factory;
public abstract interface BeanNameAware
{
public abstract void setBeanName(String paramString);
}
System.out.println("Initializing ApplicationContext");
ApplicationContext factory = new ClassPathXmlApplicationContext("bean_name.xml");
System.out.println("ApplicationContext Initialized");
SimpleBeanNameAware first = (SimpleBeanNameAware) factory.getBean("first");
System.out.println(first);
SimpleBeanNameAware second = (SimpleBeanNameAware) factory.getBean("second");
System.out.println(second);
}
}
It gives the following output,
Initializing ApplicationContext
ApplicationContext Initialized
In setBeanName : first
SimpleBeanNameAware[first]
In setBeanName : second
SimpleBeanNameAware[second]