Settings4j vs. Apache Commons Configuration

Apache Commons Configuration is the most similar library to settings4j.
Commons Configuration has also like settings4j the ability to mix configurations from heterogeneous sources and treat them like a single logic configuration.

The main differences are (as far as I know):

  • Commons configurations has more Sources/Connectors (see Compare Sources/Connectors)
  • Settings4j can also handle Objects and byte[] Content.
  • Settings4j can be accessed in a static way which made it much easier to use.
  • Settings4j has a default configuration which includes settings in System Properties, JNDI, Preferences and Classpath

Using Commons Configuration in Settings4j

Because commons configuration is older then Settings4j and had more Sources/Connectors to configure, it is an essential element to provide an Adapter to use commons-configruation in Settings4j.

With the ConfigurationToConnectorAdapter you can use every Configuration from Apache Commons Configuration as Settings4j-Connector.

An Example code could looks like the following:

public void initSettings4j() {
   String connectorName = "myCommonsConfigXmlConfigConnector";
   Connector connector = Settings4j.getSettings().getConnector(connectorName);
   if (connector == null) {
       XMLConfiguration configuration = new XMLConfiguration(new File(.....));
 
       connector = new ConfigurationToConnectorAdapter(connectorName, configuration);
 
       // add the connecter after the last SystemPropertyConnector or add it as first connector.
       Settings4j.getSettings().addConnector(connector, //
          ConnectorPositions.firstValid(//
             ConnectorPositions.afterLast(SystemPropertyConnector.class), //
             ConnectorPositions.atFirst() // if no SystemPropertyConnector is configured.
             )//
          );
   }
}

Custom Connector with Spring Placeholder

If you use the Spring-Placeholder for Settings4j, you must use a depends-on attribute to be sure that the initialization runs first:

<beans>
  <bean class="org.settings4j.helper.spring.Settings4jPlaceholderConfigurer" depends-on="initSettings4j"/>
  
  <!-- InitSettings4j.initSettings4j() will add the custom converter to the Settings4j instance. -->
  <bean id="initSettings4j" class="com.myProject.InitSettings4j" init-method="initSettings4j"/>
</beans>

Compare Sources/Connectors

Commons Configuration Settings4j
Properties files PropertiesConfiguration PropertyFileConnector
Property list files (plist) PropertyListConfiguration -
JNDI JNDIConfiguration JndiConnector
System properties SystemConfiguration SystemPropertyConnector
XML XMLConfiguration -
Windows INI files HierarchicalINIConfiguration -
JDBC Datasource DatabaseConfiguration -
java.util.prefs.Preferences - PreferencesConnector
Environment (System.getenv(...)) - EnvironmentConnector
Applet parameters AppletConfiguration -
Servlet parameters ServletConfiguration -
File System - FSConnector
Classpath - ClasspathConnector

The list maybe not complete. Everyone can implement his own Converters for Commons-Configuration or Connectors for Settings4j.

Compare provided/transformed Values

Commons Configuration Settings4j
String
byte[] content -
Object -
BigDecimal -
BigInteger -
boolean -
byte -
double -
float -
int -
long -
short -