Я действительно не понимаю проблему здесь. Я пытаюсь использовать файл свойств в моем приложении Spring. Кажется, что файл прочитан, но если я попытаюсь прочитать значения, я получаю null.Весна читает файл свойств, но значения равны
Это (часть) из моего контекста XML:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aws-context="http://www.springframework.org/schema/cloud/aws/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/cloud/aws/context
http://www.springframework.org/schema/cloud/spring-cloud-aws-context.xsd">
<tx:annotation-driven />
<context:component-scan base-package="my.package" />
<context:annotation-config />
<context:property-placeholder location="classpath:test.properties" />
<!-- OTHER STUFF HERE -->
</beans>
У меня есть test.properties под SRC/основные/ресурсов
Когда я пытаюсь запустить его, как это:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:META-INF/spring/context-test.xml")
public class SimpleTest {
@Autowired
private ApplicationContext context;
@Autowired
private Environment environment;
@Test
public void test() throws Exception {
System.out.println(environment);
System.out.println(environment.getProperty("my.test"));
}
}
выход будет:
StandardEnvironment {activeProfiles=[], defaultProfiles=[default], propertySources=[systemProperties,systemEnvironment]}
null
Итак, я предполагаю, что я должен увидеть что-то в «propertySources» там, кроме системных свойств?
Кроме того, если изменить XML-строку в этом:
<context:property-placeholder location="classpath:test.properties2" />
я получаю исключение FileNotFound, который означает, что файл сам загружается? Так почему я не могу загрузить свойства из него?
В test.properties у меня есть только это:
my.test=123456
Что может быть проблема?
Согласно документации Spring, я могу получить доступ ко всем этим через объект Environment как context.getEnvironment(). Кроме того, ваше решение работает интересно, но я предпочел бы другой путь. – breakline
Ну, я думаю, что другой путь более сложный, я просто хотел получить доступ к переменным окружения, которые определены в моей среде, но они производятся. Но, я думаю, ваше решение может работать. благодаря – breakline