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.
Let's see each one in more detail:
Sets the Configuration Identifier in the persistence layer.
@ConfigID("TestConfigurations") @ConfigSectionID("bean") public class MockupPojo { (...) }
Sets the Section Identifier in the persistence layer
@ConfigID("TestConfigurations") @ConfigSectionID("bean") public class MockupPojo { (...) }
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.
@ConfigID("TestConfigurations") public class MockupPojo { @ConfigSectionIDGetter public String getSectionID() { (...) } (...) }
Sets the key identifier for the current attribute (getter method), overriding the default inferred key.
public class MockupPojo { (...) @ConfigKeyID("FullName") public String getName() { return this.name; } (...) }
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.
public class MockupPojo { (...) @ConfigDefault("John Doe") public String getName() { return this.name; } (...) }
Instructs Configuration Utils to ignore this attribute.
No errors will be issued, it will simply be ignored.
public class MockupPojo { (...) @ConfigIgnore public String getName() { return this.name; } (...) }
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 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; } (...) }
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.