Make IntelliJ aware of a properties file

孤街浪徒 提交于 2021-01-27 05:18:27

问题


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?


回答1:


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



回答2:


This can be set in project structure. Just look at this capture.




回答3:


Create a folder named res in your project and keep the necessary property files in that folder. Go to the Project_Structures -> Modules and select the tab Resources. In your code, you can access the resource file using the path: res/your_file_name.

Before marking the folder as a resource folder

After marking the folder as a resource folder



来源:https://stackoverflow.com/questions/28365593/make-intellij-aware-of-a-properties-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!