Mr *_*gan 4 java spring dependency-injection properties-file
在我的Spring应用程序中,我有一个位于文件夹中的简单属性文件,WEB-INF\classes以便它,DispatcherServlet以及各种其他配置文件classpath.
props文件在DispatcherServletas中定义:
<bean id="propertiesFactory" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="location">            
           <value>/WEB-INF/classes/library.properties</value>
        </property>
    </bean>
将propertiesFactorybean注入控制器:
@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"));
并查看用作类路径条目的内容.
| 归档时间: | 
 | 
| 查看次数: | 44961 次 | 
| 最近记录: |