SpringBoot无法在Linux上解析占位符,在Eclipse上可以正常工作

时间:2019-06-10 10:38:18

标签: java spring-boot

我的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}"

因此,当在Linux上运行时,sp​​ring似乎找不到application.properties vars。为什么不呢?

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

任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

Eclipse内置于导出器中,用于生成jar,仅包括项目中实际生成的目标文件夹文件。

为了拥有一个“胖”,“薄”(独立的可执行文件)jar / war,您应该使用Spring Boot MavenGradle插件来“重新打包” jar / war。

This可以进一步帮助您。

答案 1 :(得分:0)

我按照@Malathi的建议使用mvn install,并使用此处建议的方法创建了一个瘦罐子: thin jar 而且效果很好,尽管我仍然不知道为什么原始方法失败了。