У меня есть веб-приложение, где у меня есть типичная проблема, которая требует разных файлов конфигурации для разных сред. Некоторая конфигурация размещается на сервере приложений как источники данных JNDI, однако некоторая конфигурация остается в файлах свойств.
Поэтому я хочу использовать функцию профилей Spring.
Моя проблема в том, что я не запускаю тестовый пример.
context.xml:
<context:property-placeholder
location="classpath:META-INF/spring/config_${spring.profiles.active}.properties"/>
Тестовое задание:
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({
TestPreperationExecutionListener.class
})
@Transactional
@ActiveProfiles(profiles = "localtest")
@ContextConfiguration(locations = {
"classpath:context.xml" })
public class TestContext {
@Test
public void testContext(){
}
}
Кажется, проблема в том, что переменная для загрузки профиля не решена:
Caused by: java.io.FileNotFoundException: class path resource [META-INF/spring/config_${spring.profiles.active}.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:181)
at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:161)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:138)
... 31 more
Текущий профиль должен быть установлен с аннотацией @ActiveProfile
. В качестве теста я не смогу использовать web.xml
. Если возможно, я бы также хотел избежать опций времени выполнения. Тест должен выполняться как есть (если это возможно).
Как правильно активировать профиль? Можно ли установить профиль с помощью context.xml? Могу ли я объявить переменную в test-context.xml, которая фактически вызывает нормальный контекст?