在我的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文件,那么为什么不使用该程序呢?
所有内容都WEB-INF/classes
添加到类路径的根目录中。因此,您只需引用您的资源即可
library.properties
或更好
classpath:library.properties
在
<property name="location">
<value>classpath:library.properties</value>
</property>
您可能会发现运行很有用
System.out.println(System.getProperty("java.class.path"));
并查看用作类路径条目的内容。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句