commons cofiguration2 基本形

短くて済む Test.java Test2.java でいきたかったけど、これだと List を任意の文字で区切れないので、長いけど、TestConfig3.java

``` TestConfig3.java
import java.io.*;
import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.commons.configuration2.BaseConfiguration;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.PropertiesConfiguration;

import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Configurations;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
import org.apache.commons.configuration2.ex.ConfigurationException;


public class TestConfig extends BaseConfiguration {
    
    private static final Logger logger = LoggerFactory.getLogger(TestConfig.class);

    private static TestConfig conf;

    private static Configurations configs;
    private static Configuration config;

    public static synchronized TestConfig getInstance() throws ConfigurationException {
        if (conf == null) {
                conf = new TestConfig();
        }
                return conf;
    }

    private TestConfig() throws ConfigurationException {
        Parameters params = new Parameters();
        FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
            new FileBasedConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class)
            .configure(params.properties()
                       .setFileName("sample.properties")
                       .setListDelimiterHandler(new DefaultListDelimiterHandler(',')));
        Configuration config = builder.getConfiguration();
        super.append(config);
    }

    public static void main(String[] args) {
        try {
            TestConfig c = TestConfig.getInstance();
            List<Object> l = c.getList(TestConfigEnum.HOGE.getParam());

            System.out.println(l.size());

            String[] ss = c.getStringArray(TestConfigEnum.HOGE.getParam());
            System.out.println(ss.length);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

```



``` Test.java
import java.io.*;
import java.util.*;

import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.builder.fluent.Configurations;


public class Test {
    public static void main(String[] args) {
        Configurations configs = new Configurations();

        try {
            Configuration c = configs.properties(new File("sample.properties"));
            System.out.println(c.getString("hoge"));

            List l = c.getList("list");
            System.out.println(l);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

```

``` Test2.java

import java.io.*;
import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.commons.configuration2.BaseConfiguration;
import org.apache.commons.configuration2.Configuration;

import org.apache.commons.configuration2.builder.fluent.Configurations;
import org.apache.commons.configuration2.ex.ConfigurationException;

public class TestConfig extends BaseConfiguration {
    /** ロガー */
    private static final Logger logger = LoggerFactory.getLogger(TestConfig.class);

    /** インスタンス */
    private static TestConfig conf;

    private static Configurations configs;
    private static Configuration config;

    /**
     * このクラスのインスタンスを取得する
     *
     * @return このクラスのインスタンス
     * @throws ConfigurationException
     */
    public static synchronized TestConfig getInstance() throws ConfigurationException {
        if (conf == null) {
            conf = new TestConfig();
        }
        return conf;
    }

    private TestConfig() throws ConfigurationException {
        this.configs = new Configurations();
        this.config = configs.properties(new File("sample.properties"));
        super.append(config);
    }

    public static void main(String[] args) {
        try {
            TestConfig c = TestConfig.getInstance();
            System.out.println(c.getString("hoge"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

```