简体   繁体   中英

How to find properties file on classpath?

My project has two modules persistence and services

persistence/MySqlDatabaseConfig looks like

@Configuration
@Profile("default")
@PropertySources({
        @PropertySource("classpath:resources/db.properties"),
        @PropertySource(value = "file:/home/y/conf/pryme/db.properties", ignoreResourceNotFound = true)
})
public class MySqlDatabaseConfig extends JpaCommonConfig {
 ....
}

services/src/main/resources/db.properties

database.driverClassName=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/newDB?createDatabaseIfNotExist=true
database.username=root
database.password=

also services module depends on persistence module

When I run tests I see error as

Failed to load bean class: com.yahoo.comma.persistence.profiles.MySqlDatabaseConfig; nested exception is java.io.FileNotFoundException: class path resource [resources/db.properties] cannot be opened because it does not exist  

How can I access properties file correctly?

UPDATE
In services.war , I see that it is here

   374 Wed Jun 11 11:26:16 PDT 2014 WEB-INF/classes/pryme.properties

src/main/resources is a default path that maven uses to holds resources.

During compilation/build times, it takes everything in there and puts them at the root of the target runtime classpath.

You need to specify the entry as

classpath:/db.properties

since it will be at the root ( / ) of the classpath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM