How to get the id of a bean from inside the bean in Spring?

How to get the id of a bean from inside the bean in Spring?

What is the easiest way to retrieve a bean id from inside that bean (in the Java code) without using a BeanPostProcessor to set a field?

The only way I can think of is something like this using a BeanPostProcessor:

public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {     ((MyBean)bean).setName(beanName);     return bean; } 

Is there a better way that doesn't require me to write an extra class or know the class of the bean in question? I tried searching through the docs and on Google, but I'm not really sure what I need to be looking for.


Relative References in Expressions in XML bean definitions in Spring 3.0


Spring ApplicationContext Bean Scope
Just implement the org.springframework.beans.factory.BeanNameAware interface and you will get it automatically.

Receiving an array of Object from a web form using Spring framework
It has one method:.
Eclipse keeps changing the Targeted Runtimes
void setBeanName(String name) 

How to use Spring to inject an object in a setter that doesnt follow the Java bean specification?

76 out of 100 based on 66 user ratings 1366 reviews