Validator retrieve property values from Bean in Spring-boot (Alternative to BeanUtils)

I want to build a validator between two fields in POJO inside my Spring-Boot app.

I searched an example how to make it and I found this code:

Object checkedValue = BeanUtils.getProperty(object, selected);

My problem is that I can’t use BeanUtils.getProperty(object, selected),

How can I get the property from my bean ?

Leave a Reply

1 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
newest oldest most voted
Notify of

If you are referring to pull specific values, you can simply use the getDeclaredFiled from the Class. A typical code block for this implementation as per the code you have will look as specified below:

Class<?> tempClass = object.getClass();
Field field = tempClass.getDeclaredField(selected);
Object checkedValue = field.get(object);

If you are referring to pull environment variables, You can use Environment from the Spring’s core package package org.springframework.core.env

If you are using annotations, simple @Autowire the Environment and you can retrieve the property like you do using BeanUtils. Typical code block would like as specified below

private Environment environment;

String value = environment.getProperty("property_name");