1

I'm trying to use an Enum to define the different profiles my Spring application might use.

This is my enum Profiles.java

public enum Profiles {

    DEVELOPMENT("dev"),
    TEST("test"),
    PRODUCTION("prod");

    private final String code;

    private Profiles(String code) {
        this.code = code;
    }
}

I'm using it in a file to configure the properties place holders.

@Configuration
public class PropertyPlaceholderConfig {

    @Profile(Profiles.DEVELOPMENT)
    public static class DevelopmentConfig {
        @Bean
        public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws IOException {
            PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
            propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(Boolean.TRUE);
            propertySourcesPlaceholderConfigurer.setLocation(new ClassPathResource("props/application-dev.properties"));
            return propertySourcesPlaceholderConfigurer;
        }
    }

    @Profile(Profiles.TEST)
    public static class TestConfig {
        @Bean
        public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws IOException {
            PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
            propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(Boolean.TRUE);
            propertySourcesPlaceholderConfigurer.setLocation(new ClassPathResource("props/application-test.properties"));
            return propertySourcesPlaceholderConfigurer;
        }
    }

    @Profile(Profiles.PRODUCTION)
    public static class ProductionConfig {
        @Bean
        public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws IOException {
            PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
            propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(Boolean.TRUE);
            propertySourcesPlaceholderConfigurer.setLocation(new ClassPathResource("application-test.properties"));
            return propertySourcesPlaceholderConfigurer;
        }
    }
}

However it is complaining at @Profile that its getting incompatible types, got Profiles expected String. I feel like I'm missing something really silly.

user253751
  • 57,427
  • 7
  • 48
  • 90
jkratz55
  • 821
  • 2
  • 13
  • 27
  • What are \@Configuration, \@Profile and \@Bean? (ignore the extra \\) – user253751 Mar 23 '15 at 23:24
  • That is from Spring libraries to configure all the components of the Spring application using different profiles, so I can customize the components and configure depending on what mode the application is running in, dev, test, prod, etc. – jkratz55 Mar 23 '15 at 23:26

2 Answers2

3

I think this will NOT work. I tried it in Java 8 and compiler complains that "Attribute value must be a constant".

As explained here it can only be primitive or String.

Possible solution is:

@Profile(SpringProfiles.TEST)
public static class SpringProfiles {
    public static final String TEST = "test"; 

}

Even thought @Profile expects String[] this works - I guess there is some implicit conversion between String and String[].

Community
  • 1
  • 1
Martin Tlachač
  • 379
  • 4
  • 17
-1

Profile expects a String array (implemented as varargs) as its argument

@Profile(Profiles.DEVELOPMENT.name())
Reimeus
  • 158,255
  • 15
  • 216
  • 276