使用 ENUM 作为配置文件,但需要一个字符串
Using ENUM for Profile, but expecting a String
我正在尝试使用枚举来定义我的 Spring 应用程序可能使用的不同配置文件。
这是我的枚举Profiles.java
public enum Profiles {
DEVELOPMENT("dev"),
TEST("test"),
PRODUCTION("prod");
private final String code;
private Profiles(String code) {
this.code = code;
}
}
我在文件中使用它来配置属性占位符。
@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;
}
}
}
但是它在@Profile 抱怨它的类型不兼容,得到的是配置文件预期的字符串。我觉得我错过了一些非常愚蠢的东西。
配置文件需要一个 String array(作为可变参数实现)作为它的参数
@Profile(Profiles.DEVELOPMENT.name())
我认为这行不通。我在 Java 8 中尝试过,编译器抱怨 "Attribute value must be a constant".
正如所解释的那样here它只能是原始类型或字符串。
可能的解决方案是:
@Profile(SpringProfiles.TEST)
public static class SpringProfiles {
public static final String TEST = "test";
}
甚至认为@Profile 期望 String[] 这有效 - 我猜 String 和 String[] 之间存在一些隐式转换。
我正在尝试使用枚举来定义我的 Spring 应用程序可能使用的不同配置文件。
这是我的枚举Profiles.java
public enum Profiles {
DEVELOPMENT("dev"),
TEST("test"),
PRODUCTION("prod");
private final String code;
private Profiles(String code) {
this.code = code;
}
}
我在文件中使用它来配置属性占位符。
@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;
}
}
}
但是它在@Profile 抱怨它的类型不兼容,得到的是配置文件预期的字符串。我觉得我错过了一些非常愚蠢的东西。
配置文件需要一个 String array(作为可变参数实现)作为它的参数
@Profile(Profiles.DEVELOPMENT.name())
我认为这行不通。我在 Java 8 中尝试过,编译器抱怨 "Attribute value must be a constant".
正如所解释的那样here它只能是原始类型或字符串。
可能的解决方案是:
@Profile(SpringProfiles.TEST)
public static class SpringProfiles {
public static final String TEST = "test";
}
甚至认为@Profile 期望 String[] 这有效 - 我猜 String 和 String[] 之间存在一些隐式转换。