简体   繁体   中英

Check dynamically if property exists in Spring application

I have an API application in Spring which receives input as an Object array. The composition of this array is controlled based on the API I invoke. Each API has a different and specific request object.

For each API call, I need to invoke an audit operation. But this should be done ONLY if a certain value exists in each request I receive. The value that I need to look for could be different for each API call.

How can I write a generic config using Spring that will dynamically check the presence of my pre-requisite value? The value I look for is a property in a Bean. The type of Bean differs with each API call

I think you should use reflection to get the value.

Class aClass = MyObject.class Field field = aClass.getField("someField");

MyObject objectInstance = new MyObject();

Object value = field.get(objectInstance);

Here is a tutorial

http://tutorials.jenkov.com/java-reflection/fields.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM