Add xml id value to spring bean

I am curious if anyone knows a quick way to achieve my goal. I want to enter the id value in a string on my spring bean.

This is what I want in a nutshell:

<bean id="matsientst" class="com.matt.Matt"/> 

 public class Matt { @Value("#id") String id; } 

The reason why I need this is because we have many objects that are configured in our spring xml, which I also need to manage in the database. spring id is a convenient key that I can use. I do not want to enter all my objects, since I abstracted them all. I could elegantly highlight the Abstract class that defines the ID. That is, if it works. thanks Matt

+4
source share
1 answer

Just implement BeanNameAware , and Spring will provide an id or name attribute (depending on which one you used), for example

 public class Matt implements BeanNameAware { private String id; public void setName(String beanName) { this.id = beanName; } } 
+7
source

Source: https://habr.com/ru/post/1389543/


All Articles