Make IntelliJ aware of a properties file

Joe Lee-Moyet picture Joe Lee-Moyet · Feb 6, 2015 · Viewed 10.1k times · Source

Is there some way to tell IntelliJ that a particular .properties file will be loaded into a project's environment? We use @PropertySource annotations to load properties files, and in several places load override values from a file determined by an already-configured property, like so:

@Configuration
@PropertySource("classpath:com/example/package/db.properties")
@PropertySource("classpath:com/example/package/db.${db.instance}.properties")
public class DatabaseConfig {
    private @Value("${db.someProperty}") String someDBProperty;
    // ...
}

The problem is that within one of these indirectly referenced files, e.g. db.test.properties, IntelliJ doesn't know if/how the properties file is referenced, so it can't tie any of the entries to their usages. (Bizarrely, some of the properties listed in these files are not greyed out to indicate 'Unused property', though even these give no results for a 'Find usages' search). There is no issue with directly named files, e.g. db.properties above.

Is there some way to tell IntelliJ about these files short of creating additional dummy @Configuration files that reference them?

Answer

javing picture javing · Jun 11, 2015

This is how I do it:

  • From the project explorer, i right click in src/main/resources, I hover on "Mark Directory As..." and I click on "Resources Root"

enter image description here

  • In the Spring configuration I just specify the name of the properties file directly.

Here an example when I am doing this in one of my projects: https://github.com/SFRJ/springpractice/blob/master/src/main/java/part3/PropertiesLoaderConfiguration.java

By the way, if you are using multiple property sources you can also do something like this:

@PropertySources({
        @PropertySource("a.properties"),
        @PropertySource("b.properties")
    })