Classpath в @PropertySource
Я использую Spring конфигурацию Java для создания bean.
Но этот bean является общим для 2 приложений.
Оба имеют один файл свойств abc.properties, но с разными местами пути к классам.
Когда я добавляю явный путь к классам, например
@PropertySource("classpath:/app1/abc.properties")
тогда он работает, но когда я пытаюсь использовать подстановочный знак, например
@PropertySource("classpath:/**/abc.properties")
тогда это не сработает.
Я стараюсь использовать множество комбинаций, но он все еще не работает.
Подстановочный знак работает в @ProeprtySource
Есть ли другой способ прочитать свойство в классе, отмеченном @Configurations
.
Ответы
Ответ 1
@PropertySource API: Resource location wildcards (e.g. **/*.properties) are not permitted; each location must evaluate to exactly one .properties resource.
обходной путь: попробуйте
@Configuration
public class Test {
@Bean
public PropertyPlaceholderConfigurer getPropertyPlaceholderConfigurer()
throws IOException {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/**/abc.properties"));
return ppc;
}
Ответ 2
Addidtionally для dmay обходной путь:
Так как Spring 3.1 PropertySourcesPlaceholderConfigurer следует использовать преимущественно над PropertyPlaceholderConfigurer, а bean должен быть статическим.
@Configuration
public class PropertiesConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
PropertySourcesPlaceholderConfigurer propertyConfigurer = new PropertySourcesPlaceholderConfigurer();
propertyConfigurer.setLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/**/abc.properties"));
return propertyConfigurer;
}
}