royjavelosa
royjavelosa

Reputation: 2068

Maven Project: Spring cannot find property file inside src/main/resources using PropertyPlaceholderConfigurer

Spring cant find my property file (MyPropFile.properties) inside src/main/resources and throws an exception like below

java.io.FileNotFoundException: class path resource [file*:/src/main/resources/MyPropFile.properties] cannot be opened because it does not exist

But if I place MyPropFile.properties at the root of my project (MyProject/MyPropFile.properties) spring can find it and the programs executes properly.

How do I configure this so that I can place my .properties file inside src/main/resources

this is my namespace

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
    http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd
    ">

this is my bean

<context:property-placeholder location="classpath:MyPropFile.properties" />

Java:

@Value("${message.fromfile}")
      private String message;

Thanks in advance guys.

Upvotes: 0

Views: 11466

Answers (4)

tmarwen
tmarwen

Reputation: 16374

As an addendum to @ryanp solution which is the correct and sanitized way to deal with files resources, which should be under the classpath location.

Maven will automagically collect the configured file resources and append them to your runtime classpath holder so that resources prefixed with classpath: can be resolved.

Otherwise, if you find your self stack with Maven not being able to stream file resources, you may hook into Maven's resources configuration and map your files location as follows:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>*.properties</include>
            </includes>
        </resource>
    </resources>
</build>

Upvotes: 0

Debojit Saikia
Debojit Saikia

Reputation: 10632

Try this. Make this entry in your application config file:

<beans xmlns:context="http://www.springframework.org/schema/context"
 .....
 xsi:schemaLocation="...
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd
 ....>

<context:property-placeholder location="classpath:MyPropFile.properties" />
 ....
</beans>

and access the message property:

@Value("${messageFromfile}")
private String message;

Upvotes: 2

ryanp
ryanp

Reputation: 5127

You should use

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
            <value>classpath:/MyPropFile.properties</value>
        </property>
</bean>

Without the classpath: prefix, the PropertyPlaceholderConfigurer is attempting to resolve the resource as a file resource, so is looking for it in your current working directory.

Upvotes: 1

Brian Agnew
Brian Agnew

Reputation: 272357

I'd expect Maven to copy this to the target directory and for the classpath to be set appropriately. I wouldn't expect Maven to search the source directory other than at compile time.

Upvotes: 1

Related Questions