Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java spring boot在命令行上运行时不读取application.properties文件,但在STS中工作_Java_Spring_Spring Boot_Spring Data Jpa - Fatal编程技术网

Java spring boot在命令行上运行时不读取application.properties文件,但在STS中工作

Java spring boot在命令行上运行时不读取application.properties文件,但在STS中工作,java,spring,spring-boot,spring-data-jpa,Java,Spring,Spring Boot,Spring Data Jpa,我正在使用STS4IDE开发一个带有SpringBoot的桌面应用程序。在STS中一切正常,我有不同配置文件的不同属性文件,主属性文件application.properties都在resources文件夹中 src/main/resources application.properties application-dev.properties application-prod.properties 在application.properties中,我定义了要扫描的环境和包,以查找两个应用程序

我正在使用STS4IDE开发一个带有SpringBoot的桌面应用程序。在STS中一切正常,我有不同配置文件的不同属性文件,主属性文件application.properties都在resources文件夹中

src/main/resources
application.properties
application-dev.properties
application-prod.properties
application.properties中,我定义了要扫描的环境和包,以查找两个应用程序数据库的实体:

spring.profiles.active=dev
wms.basepackage=com.wms
app.basepackage=es.app
我使用boot dasboard运行应用程序,一切正常。问题是在导出JAR时,我使用指令从命令执行它

java -jar -Dspring.profiles.active=dev RunableApp4.jar
应用程序失败,出现以下错误:

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [es.app.InterfaceApplication]; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'wms.basepackage' in value "${wms.basepackage}"
我在日志中注意到,从命令行运行应用程序的步骤与使用STS运行应用程序的步骤有所不同:

STS步骤(使用引导仪表板):

ConfigFileApplicationListener进入场景并加载主应用程序 application.properties文件和application-dev.properties环境文件。然后,RepositoryConfigurationDelegate将正确加载存储库

命令行步骤:

java -jar -Dspring.profiles.active=dev RunableApp4.jar
13:07:49.453 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Found key 'spring.profiles.active' in PropertySource 'systemProperties' with value of type String
13:07:49.455 [main] DEBUG org.springframework.core.env.StandardEnvironment - Activating profiles [dev]
13:07:49.455 [main] DEBUG org.springframework.core.env.StandardEnvironment - Activating profiles [dev]
3:07:49.537 [main] INFO es.app.InterfaceApplication - Starting InterfaceApplication on matias-pc with PID 32173 (/home/matias/Documentos/JARS/RunableApp4.jar started by matias in /home/matias/Documentos/JARS)
13:07:49.537 [main] DEBUG es.app.InterfaceApplication - Running with Spring Boot, Spring
13:07:49.537 [main] INFO es.app.InterfaceApplication - The following profiles are active: dev
13:07:49.538 [main] DEBUG org.springframework.boot.SpringApplication - Loading source class es.app.InterfaceApplication
13:07:49.580 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7a1ebcd8
13:07:49.584 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
13:07:49.642 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/home/matias/Documentos/JARS/RunableApp4.jar!/mx/gm3s/configuracion/ConfiguracionDSErp.class]
.
.
Read candidate components
13:07:49.861 [main] WARN org.springframework.context.annotation.AnnotationConfigApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [es.app.InterfaceApplication]; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'wms.basepackage' in value "${wms.basepackage}"
13:07:49.864 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [es.app.InterfaceApplication]; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'wms.basepackage' in value "${wms.basepackage}"
ConfigFileApplicationListenerRepositorConfigurationDelegate未到达现场,注释ConfigApplicationContext开始扫描组件,并由于无法解析属性值而失败

我已尝试将属性文件作为参数发送,但结果相同:

java -jar -Dspring.profiles.active=dev  -Dspring.config.location=file:/InterfaceApp/src/main/resources/application.properties RunableApp4.jar
主要课程包括:

@SpringBootApplication
@Import(ConfigInterfaceApp.class)
@EnableScheduling
public class InterfaceApp  {

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


@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = {"${wms.basepackage}", "${app.basepackage}"})
public class ConfigInterfaceApp {

}

我做错了什么

在应用程序中读取
application.properties
,如下所示:

ClassLoader.class.getResourceAsStream("/path/to/application.properties");
或者,

ClassLoader.getResourceAsStream("/application.properties");

阅读应用程序中的
application.properties
,如下所示:

ClassLoader.class.getResourceAsStream("/path/to/application.properties");
或者,

ClassLoader.getResourceAsStream("/application.properties");
你能试试这个吗

java -jar RunableApp4.jar --spring.config.location=<relative-path-to-configs>/InterfaceApp/src/main/resources/ --spring.profiles.active=dev
java-jar RunableApp4.jar--spring.config.location=/interfaceeapp/src/main/resources/--spring.profiles.active=dev
你能试试这个吗

java -jar RunableApp4.jar --spring.config.location=<relative-path-to-configs>/InterfaceApp/src/main/resources/ --spring.profiles.active=dev
java-jar RunableApp4.jar--spring.config.location=/interfaceeapp/src/main/resources/--spring.profiles.active=dev

您确定
.properties
文件也打包在JAR中吗?试着打开它看看它们是否在那里是的,application.properties和application-dev.properties都在/RunableApp4根目录中您确定
.properties
文件也在JAR中打包吗?试着打开它看看它们是否在那里是的,application.properties和application-dev.properties都在/RunableApp4根目录中