Using Configuration Utils Annotations

Like you saw in the Using Java Beans directly with Configuration Utils Section, there are a few drawbacks on the convention that drives the Java Bean approach. These are here tackled by annotating/configuring those exceptions on the conventioned behaviour.

Existing annotations provided and supported by Configuration Utils

  • @ConfigID: Sets the Configuration Identifier in the persistence layer
  • @ConfigSectionID: Sets the Section Identifier in the persistence layer
  • @ConfigSectionIDGetter: Sets the Section Identifier in the persistence layer, by calling a method in write execution time.
  • @ConfigKeyID: Sets the key identifier for the current attribute (getter method)
  • @ConfigDefault: Sets a default value for this attribute if none exists in the repository.
  • @ConfigIgnore: Instructs Configuration Utils to ignore this attribute. No errors will be issued, it will simply be ignored.

Let's see each one in more detail:

@ConfigID

Sets the Configuration Identifier in the persistence layer.

Usage:
@ConfigID("TestConfigurations")
@ConfigSectionID("bean")
public class MockupPojo {
  
  (...)

}

@ConfigSectionID

Sets the Section Identifier in the persistence layer

Usage:
@ConfigID("TestConfigurations")
@ConfigSectionID("bean")
public class MockupPojo {
  
  (...)

}

@ConfigSectionIDGetter

Sets the Section Identifier in the persistence layer, by calling a method in write execution time.

This allows for dynamic Section IDs to be supported. The Bean itself can implement a Section selection logic in any method annotated with this annotation. Only one thing is enforced, the method must return a String value, the ID to use.

Usage:
@ConfigID("TestConfigurations")
public class MockupPojo {

  @ConfigSectionIDGetter
  public String getSectionID() {
      (...)
  }
  
  (...)
}

@ConfigKeyID

Sets the key identifier for the current attribute (getter method), overriding the default inferred key.

Usage:
public class MockupPojo {

  (...)
  
  @ConfigKeyID("FullName")
  public String getName() {
      return this.name;
  }
  
  (...)
}

@ConfigDefault

Sets a default value for this attribute if none exists in the repository.

It will always be a String value that will be converted to the corresponding data type.

Usage:
public class MockupPojo {

  (...)
  
  @ConfigDefault("John Doe")
  public String getName() {
      return this.name;
  }
  
  (...)
}

@ConfigIgnore

Instructs Configuration Utils to ignore this attribute.

No errors will be issued, it will simply be ignored.

Usage:
public class MockupPojo {

  (...)
  
  @ConfigIgnore
  public String getName() {
      return this.name;
  }
  
  (...)
}

Simplified usage on Annotated POJOs

When using annotated POJOs we can use the simplified read and write methods of Configuration Utils.

The examples of the Using Java Beans directly with Configuration Utils Section could be rewritten to the following form:

The annotated POJO

The same Java Bean as before but now enriched with the annotations.

@ConfigID("TestConfigurations")
@ConfigSectionID("bean")
public class MockupPojo {

    /** Name */
    private String name;
    
    (...)
    
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    
    (...)
}

The simplified read/write code

 1  IConfigurations configAPI = new ConfigurationsPreferencesImpl();
 2
 3  MockupPojo bean = new MockupPojo();
 4  bean.setName("Abraham Lincoln");
 5  bean.setAddress("Washington DC - President's Street 1");
 6  bean.setAge(300);
 7  bean.setBirthDate(new GregorianCalendar(2007, 10, 2).getTime());
 8
 9  configAPI.writeConfiguration(bean));
10
11  bean = config.readConfiguration(MockupPojo.class);

See how easier life can be? :-D

Of course this is still nothing. We did not use the default value, dynamic section getter, the ignore and other useful features in this example.

But concerning boilerplate code... there is none left. This is the main point we want to make here.