Java.io.FileNotFoundException: class path resource [WEB-INF / classes / library.properties] cannot be opened because it does not exist

In my Spring application, I have a simple properties file located in a folder WEB-INF\classes, so that it DispatcherServletand other other configuration files are in classpath.

The props file is defined DispatcherServletas:

<bean id="propertiesFactory" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="location">            
           <value>/WEB-INF/classes/library.properties</value>
        </property>
    </bean>

propertiesFactory The bean is entered into the controller:

@Autowired 
private Properties propertiesFactory;

And it is used in one of the controller methods as:

if (adminPassword.equals(propertiesFactory.getProperty("adminPassword"))) {           

All this works fine, with the exception of a test program that has a line:

ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("library-servlet.xml");

What I chose BeanCreationException:

Injection of autowired dependencies failed

Because of:

java.io.FileNotFoundException: class path resource [WEB-INF/classes/library.properties] cannot be opened because it does not exist

But if the whole application can see the props file, why not this program?

+4
1

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"));

, .

+6

Source: https://habr.com/ru/post/1547330/


All Articles