GooglePro
GooglePro

Reputation: 33

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 ?

Upvotes: 1

Views: 453

Answers (1)

Coder
Coder

Reputation: 2239

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);
field.setAccessible(true);
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

@Autowired
private Environment environment;

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

Upvotes: 1

Related Questions