【发布时间】:2014-08-27 04:48:42
【问题描述】:
在我的 Spring 应用程序中,我有一个简单的属性文件位于文件夹 WEB-INF\classes 中,因此它、DispatcherServlet 和其他各种配置文件都在 classpath 中。
props 文件在DispatcherServlet 中定义为:
<bean id="propertiesFactory" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location">
<value>/WEB-INF/classes/library.properties</value>
</property>
</bean>
propertiesFactory bean 被注入到控制器中:
@Autowired
private Properties propertiesFactory;
并在控制器的方法之一中使用:
if (adminPassword.equals(propertiesFactory.getProperty("adminPassword"))) {
这一切都很完美,除了一个测试程序如下:
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("library-servlet.xml");
这会抛出一个BeanCreationException:
Injection of autowired dependencies failed
因为:
java.io.FileNotFoundException: class path resource [WEB-INF/classes/library.properties] cannot be opened because it does not exist
但是如果整个应用程序都可以看到props文件,那为什么不是这个程序呢?
【问题讨论】:
-
library-servlet.xml 文件中有什么...???
-
我在询问 library-servlet.xml 的内容
-
与任何其他
DispatcherServlet非常相似,即 JavaBean 注册以及所有 Spring 和 Hibernate 配置数据。你想知道具体的事情吗?
标签: java spring dependency-injection properties-file