无法打开Spring @PropertySource,因为它不存在

时间:2019-02-19 11:17:14

标签: java spring intellij-idea

我有简单的spring应用程序。 Spring的版本为3.2.16.RELEASE

项目结构为:

project structure

应用程序类如下:

@Configuration
@ComponentScan(basePackages = "ua.xxxx.spring.hometask")
@PropertySource(value = "classpath:application.properties")
public class Application {

    public static void main(final String[] args) {
        final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Application.class);

        final SpringHomeTaskConsoleUI consoleUI = context.getBean(SpringHomeTaskConsoleUI.class);

        consoleUI.run();
    }
}

当我尝试运行它时,我有:

Caused by: java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist

我正在尝试:

@PropertySource(value = "classpath*:application.properties")
@PropertySource(value = "classpath:/application.properties")
@PropertySource(value = "classpath:src/main/resources/application.properties")
@PropertySource(value = "classpath*:src/main/application.properties")
@PropertySource(value = "classpath:application.properties")
@PropertySource(value = "application.properties")
@PropertySource(value = "src/main/application.properties")

和其他...

Resources文件夹被标记为资源根目录... Intellij项目结构还可以。

任何想法可能有什么问题? 我错过了什么吗?

多谢指教!

1 个答案:

答案 0 :(得分:1)

尝试添加绝对路径文件位置@PropertySource(“ file:/ root /.../ application.properties”)