Convert a comma separated list into a List of Java Enum using Spring

publish on

Convert a comma separated list from a properties file to a List of Java Enum using Spring

One of the requirement we had was to be able to configure one or more entities (represented by an Enum) using a comma separated list in a properties file.

Example of comma separated list in a properties file:


As we are using Spring Framework, I used a bean factory method to call a method named parseProgramCodeList that parsed the list and converted it to a Java list of Enum:

public static List parseProgramCodeList(String programCodeList) {
        List programCodesEnum = new ArrayList();
        String[] programCodes = programCodeList.split(",");
        for (String programCode : programCodes) {
            ProgramCodeEnum companyProgramCode = ProgramCodeEnum.valueOfCode(programCode.trim());
        return programCodesEnum;

In the ProgramCodeEnum Enum, I defined a method taking a programCode value and returning the corresponding ProgramCodeEnum:

    public static ProgramCodeEnum valueOfCode(String value) {

        if (StringUtils.isEmpty(value)) {
            return null;

        for (ProgramCodeEnum v : values()) {
            if (v.code.equals(value)) {
                return v;

        throw new IllegalArgumentException("no enum value found for code: " + value);

And finally the Spring configuration to inject the Enum list:

    <bean id="beanId" class="com.inoneo.test">
        <property name="processProgramCodes">
            <bean factory-method="parseProgramCodeList" class="com.inoneo.test">
                <constructor-arg value="${process.only.programcode}" />

With processProgramCodes being an ArrayList:

private List<ProgramCodeEnum> processProgramCodes;
setProcessProgramCodes(List<ProgramCodeEnum> processProgramCodes){
	this.processProgramCodes = processProgramCodes;
comments powered by Disqus