英文:
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 ...
@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.
Any hints appreciated.
<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>
答案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("origin");
答案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>
英文:
Hi reading your config I can suggest you to:
- use
<properties>...<java.version>11</java.version> </properties>
probably your maven settings works but in this configuration was what the spring boot project generator suggest me to use - delete
<resources>....</resources>
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("${test}")
private String test;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(test);
}
}
the pom is this:
<?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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论