Search code examples
dependency-injectionapache-camelblueprint-osgi

When "@PropertyInject" fields are injected, want to check injected values


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)?


Solution

  • 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;
    }