class path resource [application.properties] cannot be opened because it does not exist

huangapple go评论87阅读模式
英文:

class path resource [application.properties] cannot be opened because it does not exist

问题

I saw many questions about this but can't seem to make it work: it either can't find properties file or throws Could not resolve placeholder ...

    @Slf4j
    @SpringBootApplication
    @ComponentScan(basePackages = "com.company")
    @PropertySource(value={"application.properties"})
    public class ConsumingRestApplication implements ApplicationRunner {
    
        @Autowired
        private ApplicationContext appContext;
    
        @Value("${origin}")
        private String origin;
    
        @Value("${destination}")
        private String destination;
...

I tried different paths, including absolute path for PropertySource and also ran without it at all.

My pom.xml includes resource and I tried without it also. Java is 11.0.7.

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.company</groupId>
  <artifactId>armstrong</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>armstrong</name>
  <url>http://maven.apache.org</url>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.2.RELEASE</version>
  </parent>

    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.6</version>
    </dependency>

    <dependency>
      <groupId>com.konghq</groupId>
      <artifactId>unirest-java</artifactId>
      <version>3.7.04</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.12</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.30</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.30</version>
<!--      <scope>test</scope>-->
    </dependency>
  </dependencies>

  <properties>
    <maven.compiler.source>1.11</maven.compiler.source>
    <maven.compiler.target>1.11</maven.compiler.target>
  </properties>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>

    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
          <include>**/*.properties</include>
        </includes>
      </resource>
    </resources>
  </build>
</project>
英文:

I saw many questions about this but can't seem to make it work: it either can't find properties file or throws Could not resolve placeholder ...

class path resource [application.properties] cannot be opened because it does not exist

    @Slf4j
    @SpringBootApplication
    @ComponentScan(basePackages = &quot;com.company&quot;)
    @PropertySource(value={&quot;application.properties&quot;})
    public class ConsumingRestApplication implements ApplicationRunner {
    
        @Autowired
        private ApplicationContext appContext;
    
        @Value(&quot;${origin}&quot;)
        private String origin;
    
        @Value(&quot;${destination}&quot;)
        private String destination;
...

I tried different paths, including absolute path for PropertySource and also ran without it at all.

My pom.xml includes resource and I tried without it also. Java is 11.0.7.

Any hints appreciated.

     &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;groupId&gt;com.company&lt;/groupId&gt;
  &lt;artifactId&gt;armstrong&lt;/artifactId&gt;
  &lt;packaging&gt;jar&lt;/packaging&gt;
  &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  &lt;name&gt;armstrong&lt;/name&gt;
  &lt;url&gt;http://maven.apache.org&lt;/url&gt;

  &lt;parent&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
    &lt;version&gt;2.3.2.RELEASE&lt;/version&gt;
  &lt;/parent&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;com.google.code.gson&lt;/groupId&gt;
      &lt;artifactId&gt;gson&lt;/artifactId&gt;
      &lt;version&gt;2.8.6&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;com.konghq&lt;/groupId&gt;
      &lt;artifactId&gt;unirest-java&lt;/artifactId&gt;
      &lt;version&gt;3.7.04&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
      &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
      &lt;artifactId&gt;lombok&lt;/artifactId&gt;
      &lt;version&gt;1.18.12&lt;/version&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-log4j2&lt;/artifactId&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
      &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
      &lt;version&gt;1.7.30&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
      &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
      &lt;version&gt;1.7.30&lt;/version&gt;
&lt;!--      &lt;scope&gt;test&lt;/scope&gt;--&gt;
    &lt;/dependency&gt;
  &lt;/dependencies&gt;

  &lt;properties&gt;
    &lt;maven.compiler.source&gt;1.11&lt;/maven.compiler.source&gt;
    &lt;maven.compiler.target&gt;1.11&lt;/maven.compiler.target&gt;
  &lt;/properties&gt;

  &lt;build&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;

    &lt;resources&gt;
      &lt;resource&gt;
        &lt;directory&gt;src/main/resources&lt;/directory&gt;
        &lt;filtering&gt;true&lt;/filtering&gt;
        &lt;includes&gt;
          &lt;include&gt;**/*.properties&lt;/include&gt;
        &lt;/includes&gt;
      &lt;/resource&gt;
    &lt;/resources&gt;
  &lt;/build&gt;
&lt;/project&gt;

答案1

得分: 1

另一个可以帮助你的替代方法是:

@Autowired
private Environment env;
String origin = env.getProperty("origin");
英文:

another alternative that can help you is :

@Autowired
private Environment env;
String origin = env.getProperty(&quot;origin&quot;);

答案2

得分: 1

以下是翻译好的部分:

  • 建议您使用以下配置:<properties>...<java.version>11</java.version> </properties>,可能您的Maven设置可以工作,但在这个配置中,Spring Boot项目生成器建议我使用这个配置。
  • 删除<resources>....</resources> Maven标签,使用@PropertySource注解。您的问题可能出现在其他地方。特别是如果您将配置放在application.properties中,这些配置将加载到Spring环境抽象中,您不需要添加其他内容来获取这些配置。
  • 注意您的项目树,我看到一些奇怪的东西,application.properties与典型的Spring Boot项目不一致,我还看到src文件夹位于另一个文件夹下,我猜测可能发生了一些项目设置问题。

您能否分享更详细的应用程序树的截图?pom.xml在哪里?

我测试了一个类似的场景:

@SpringBootApplication
public class DemoApplication implements ApplicationRunner {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Value("${test}")
    private String test;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println(test);
    }
}

pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

class path resource [application.properties] cannot be opened because it does not exist
class path resource [application.properties] cannot be opened because it does not exist
class path resource [application.properties] cannot be opened because it does not exist

英文:

Hi reading your config I can suggest you to:

  • use &lt;properties&gt;...&lt;java.version&gt;11&lt;/java.version&gt; &lt;/properties&gt; probably your maven settings works but in this configuration was what the spring boot project generator suggest me to use
  • delete &lt;resources&gt;....&lt;/resources&gt; maven tag with @PropertySource annotation. your problem is somewhere else. In particular if you put your configuration inside application.properties those config was loaded in the Spring Environment abstraction and you do not need to add other stuff in order to get those configurations.
  • pay attention to your project tree I saw something of strange, application.properties is not brendized with the typical spring boot project and I saw that the src folder is under another folder I guess that some project setup issue are happened.

Can you share a screenshot more detailed of the your app three. Where is the pom.xml?

I tested a your similar scenario:

@SpringBootApplication
public class DemoApplication implements ApplicationRunner {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Value(&quot;${test}&quot;)
    private String test;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println(test);
    }
}

class path resource [application.properties] cannot be opened because it does not exist
class path resource [application.properties] cannot be opened because it does not exist
class path resource [application.properties] cannot be opened because it does not exist

the pom is this:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;parent&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
        &lt;version&gt;2.3.2.RELEASE&lt;/version&gt;
        &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
    &lt;/parent&gt;
    &lt;groupId&gt;com.example&lt;/groupId&gt;
    &lt;artifactId&gt;demo&lt;/artifactId&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
    &lt;name&gt;demo&lt;/name&gt;
    &lt;description&gt;Demo project for Spring Boot&lt;/description&gt;

    &lt;properties&gt;
        &lt;java.version&gt;1.8&lt;/java.version&gt;
    &lt;/properties&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
        &lt;/dependency&gt;

    &lt;/dependencies&gt;

    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;

&lt;/project&gt;

huangapple
  • 本文由 发表于 2020年8月13日 02:12:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63382477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定