Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do I access inner beans in Spring?

As you know Java inner classes are defined within the scope of other classes, similarly, inner beans are beans that are defined within the scope of another bean. Thus, a <bean/> element inside the <property/> or <constructor-arg/> elements is called inner bean and it is shown below.

What is id in bean?

Every bean has one or more ids (also called identifiers, or names; these terms refer to the same thing). These ids must be unique within the container the bean is hosted in. A bean will almost always have only one id, but if a bean has more than one id, the extra ones can essentially be considered aliases.

What happens when inner bean is defined with id?

An inner bean definition does not require a defined id or name; if specified, the container does not use such a value as an identifier. The container also ignores the scope flag on creation: Inner beans are always anonymous and they are always created with the outer bean.


Just implement the org.springframework.beans.factory.BeanNameAware interface and you will get it automatically. It has one method:

void setBeanName(String name)