使IntelliJ知道属性文件

Joe*_*yet 6 java spring intellij-idea properties-file

有没有办法告诉IntelliJ特定.properties文件将被加载到项目的环境中?我们使用@PropertySource注释来加载属性文件,并在几个地方加载由已配置属性确定的文件中的覆盖值,如下所示:

@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;
    // ...
}
Run Code Online (Sandbox Code Playgroud)

问题是在其中一个间接引用的文件中,例如db.test.properties,IntelliJ不知道是否/如何引用属性文件,因此它不能将任何条目绑定到它们的用法.(奇怪的是,这些文件中列出的某些属性并不是灰色的,表示"未使用的属性",但即使这些属性也没有为"查找用法"搜索提供任何结果).直接命名的文件没有问题,例如db.properties上面.

有没有办法告诉IntelliJ这些文件是否创建@Configuration了引用它们的额外虚拟文件?

sfr*_*frj 4

我就是这样做的:

  • 在项目资源管理器中,我右键单击 src/main/resources,将鼠标悬停在“将目录标记为...”上,然后单击“资源根目录”

在此输入图像描述

  • 在 Spring 配置中,我直接指定属性文件的名称。

这是我在一个项目中执行此操作时的示例: https://github.com/SFRJ/springpractice/blob/master/src/main/java/part3/PropertiesLoaderConfiguration.java

顺便说一句,如果您使用多个属性源,您也可以执行以下操作:

@PropertySources({
        @PropertySource("a.properties"),
        @PropertySource("b.properties")
    })
Run Code Online (Sandbox Code Playgroud)