我正在尝试使用一个枚举来定义我的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抱怨它的类型不兼容,Profiles期望使用String。我觉得我真的很想念一些东西。
Profile期望一个String数组(以varargs实现)作为其参数
@Profile(Profiles.DEVELOPMENT.name())
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句