我的 SpringBoot 2.0.3 java 8 程序在 Eclipse Photon(使用 Maven)上运行良好,但是,当我将其构建为 jar 文件并安装在 AWS Linux 上并在那里运行时,我得到:
2019-06-10 10:07:14.395 WARN 18316 --- [ main]s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ccWrapperApplication': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'tmp.dir' in value "${tmp.dir}"
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ccWrapperApplication': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'tmp.dir' in value "${tmp.dir}"
所以看起来 spring 在 Linux 上运行时找不到 application.properties 变量。为什么不呢?
tmp.dir 在 src/main/resources/application.properties 中定义为:
tmp.dir=tmp
并且 ccWrapperApplication.java 有:
@SpringBootApplication
// SpringBoot likes to use a database, we don't need one, so we exclude the default dB.
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class CcWrapperApplication implements CommandLineRunner {
@Value("${tmp.dir}")
public String tmpDir;
public static void main(String[] args) {
logger.info("running CcWrapperApplication on " + System.getProperty("os.name"))
SpringApplication.run(CcWrapperApplication.class, args);
}
@Override
public void run(String... args) {
...
}
public String getTmpDir() {
return tmpDir;
}
public void setTmpDir(String tmpDir) {
this.tmpDir = tmpDir;
}
}
我使用 eclipse 构建了应用程序 jar: 导出->可运行的 JAR 文件
在 Linux 上安装了 jar,将 jar 添加到 CLASSPATH(以及所有 SpringBoot jar)并使用以下命令从 shell 脚本运行:
java com.clarivate.singulatiry.chemworkbench.chemcurator.chemcurator_wrapper.CcWrapperApplication $inputDir $outputDir 2>&1 >>$logfile | tee -a $logfile >>$errorfile
感谢任何帮助。
最佳答案
Eclipse
内置导出器生成jar
仅包含项目中实际生成的目标文件夹文件。
为了拥有一个“胖”、“瘦”(独立可执行)jar/war,你应该使用 Spring Boot Maven
或 Gradle
插件来“重新打包” jar / war 。
This可以进一步帮助您。
关于java - Spring Boot 无法解析 Linux 上的占位符,但在 Eclipse 上工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56524768/