Kapitalny
Kapitalny

Reputation: 711

Spring Environment properties validator

I'm trying to make some kind of all Environment properties validator. What I want is just take all properties from Environment.

(I already found this here: Spring: access all Environment properties as a Map or Properties object)

Next on all properties I want to make environment.getProperty(x) And If some properties are null then just I want add them and throw as message of Excpetion.

That works pretty well, but.. Checking of all dependencies is performing after logger initialization and after jpa/hibernate initialization.

Can I somehow get with my "validation mechanizm" before that actions and with access to Environment bean? Or maybe there is way to take Environment without running application?

Upvotes: 0

Views: 171

Answers (1)

rado
rado

Reputation: 105

  1. As I know, there is no any mechanism that check by NULL all you properties. If you use Java 8, you can filter NULL properties from your Map. For example your properties collected in Map yourPropertiesMap:

a)

Map<String, String> notNullPropertiesMap = yourPropertiesMap.entrySet()
            .stream()
            .filter(entry -> Objects.nonNull(entry.getValue()))
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));  

b)

Map<Boolean, List<Map.Entry<Object, Object>>> splitPropertiesMap = yourPropertiesMap.entrySet()
            .stream()
            .collect(Collectors.partitioningBy(entry -> Objects.nonNull(entry.getValue())));

In second example splitPropertiesMap.get(Boolean.TRUE) will return you list of all not NULL properties Map.

  1. I think it's some kind of strange question, you need to check your properties.

Anyway I hope it'll help you to simplify your code.

Upvotes: 1

Related Questions