java - Spring Boot 无法解析 Linux 上的占位符,但在 Eclipse 上工作正常

标签 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}"

所以看起来 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 MavenGradle 插件来“重新打包” jar / war 。

This可以进一步帮助您。

关于java - Spring Boot 无法解析 Linux 上的占位符,但在 Eclipse 上工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56524768/

相关文章:

java - JAXB:编码的 XML 中缺少具体类型信息 (xsi:type)

java - 即使依赖 Bean 存在,创建 Bean 时出错

java - SpringBoot中mocking时出现NullPointerException

java - 发布在不持有用于发布的锁的情况下构造的 @NotThreadSafe 实例是否安全?

java - 服务器端推送技术-数据库更改后

Spring Boot OAuth2隐式流程+表单登录和请求方法 'POST'不支持错误

java - 如何使用 Spring 配置文件模拟实现相同接口(interface)的不同服务

java - JPA自定义转换器中的空指针异常 - Spring boot

java - 如何阻止在非渲染 JSF 组件中计算 EL 表达式?

java - OSGi/Eclipse RCP 环境中的 Resteasy 客户端