2

I am using @PropertyInject in some of my classes. Those classes are inject via blueprint (using maven blueprint plugin). I want to check the values that are injected into fields with @PropertyInject.

The problem is that within PostConstruct (supported by maven-blueprint-plugin -> init method) all fields are still null. But if i use the object (a camel endpoint) that injects those fileds all fields are set properly.

So between "PostConstruct" and usage of the instance all fields get injected. Is there a way i can hook directly after the injection to check the values (!=null)?

dermoritz
  • 12,519
  • 25
  • 97
  • 185
  • Basically you want to ensure that you are not injecting something rather than empty or null? I am not sure you can do that via the properties set in blueprint. I would think you need to check this yourself and throw an exception if the property should have a value. – Souciance Eqdam Rashti Dec 07 '16 at 15:15
  • i want check it! but when? I want to check in the object that injects those fields. This object is then injected into another object. – dermoritz Dec 08 '16 at 08:33
  • Can't you just use some defensive programming to check if the field is empty or not before doing anything with it? – Souciance Eqdam Rashti Dec 08 '16 at 08:36
  • I WANT TO USE DEFENSIVE PROGRAMMING! But where in the code? I want to check right after fields are injected but how? (as i stated constructor and postconstruct are to early) – dermoritz Dec 08 '16 at 09:52
  • 2
    First of all, shouting won't get you anywhere. Like I mentioned in the comment, I highly doubt you can verify straight after injection, rather you would need verify the content of the variable when you want to use the variable or whenever you refer to it. If that's not suitable to your requirement post the question on the Camel Nabble forum to see if you can get a better answer. – Souciance Eqdam Rashti Dec 08 '16 at 10:00
  • Sorry, but "defensive" means for me to check at most early point in time. At the moment i can only check them if used - in every consumer of the class. So my question ist how the Camel life cycle looks like to find a method or a way to check the parameter right after injection. – dermoritz Dec 08 '16 at 12:04

1 Answers1

2

Put @PropertyInject on the setter and check the value being set in the setter.

@PropertyInject("prop")
public void setProp(String value) {
  if (value == null) {
    throw new IllegalArgumentException("prop cannot be null");
  }
  this.prop = value;
}
Strelok
  • 50,229
  • 9
  • 102
  • 115