无法从战争文件的外侧找到application.properties文件

时间:2019-11-29 04:42:10

标签: java spring spring-boot spring-data-jpa

在将application.properties文件外部化的过程中进行侧向战争。当我通过詹金斯创建战争文件时显示错误。

  

启动ApplicationContext时出错。显示条件报告   在启用“调试”的情况下重新运行您的应用程序。 2019-11-28 18:06:07.900   错误22612-[[main]   o.s.b.d.LoggingFailureAnalysisReporter:

     
     

申请无法开始

     
     

说明:

     

无法配置数据源:未指定'url'属性,并且   无法配置嵌入式数据源。

     

原因:无法确定合适的驱动程序类别

     

操作:

     

请考虑以下事项:如果要嵌入式数据库(H2,HSQL或   Derby),请将其放在类路径上。如果您有数据库设置   要从特定配置文件加载您可能需要激活它(否   个人资料当前处于活动状态)。

     

2019-11-28 18:06:07.909错误22612 --- [main]   o.s.test.context.TestContextManager:捕获异常   允许TestExecutionListener   [org.springframework.test.context.web.ServletTestExecutionListener@233795b6]   准备测试实例   [com.assessmentAggregator.api.AssessmentAggregatorApiApplicationTests@74024f3]

     

java.lang.IllegalStateException:无法在以下位置加载ApplicationContext   org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)   〜[spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE]在   org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)   〜[spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE]在   org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)   〜[spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE]在   org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)   〜[spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE]在   org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)   〜[spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE]在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)[spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE]在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner $ 1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)   [spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE]在   org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)   [junit-4.12.jar:4.12]位于   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)   [spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE]在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)   [spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE]在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)   [spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE]在   org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)   [junit-4.12.jar:4.12]位于   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)   [junit-4.12.jar:4.12]位于   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)   [junit-4.12.jar:4.12]位于   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)   [junit-4.12.jar:4.12]位于   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)   [junit-4.12.jar:4.12] at

为了外部化属性文件,我使用了这段代码。

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    return builder
            .properties("spring.config.name:" + AppConstants.Config.APPLICATION_PROPERTY_NAME,
                    "spring.config.location:" + AppConstants.Config.APPLICATION_PROPERTY_SOURCE_PATH)
            .sources(AssessmentAggregatorApiApplication.class);
}

和的值       AppConstants.Config.APPLICATION_PROPERTY_NAME= String APPLICATION_PROPERTY_NAME = "foxmatrixapp.properties";
AppConstants.Config.APPLICATION_PROPERTY_SOURCE_PATH=String APPLICATION_PROPERTY_SOURCE_PATH = "file:////mnt/assessment_aggregator/data/pdf/";
并将此代码添加到pom.xml文件中,以便从我使用的war文件中排除application.properties文件。

 <resources>
     <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <excludes>
            <exclude>**/*.properties</exclude>
        </excludes>
    </resource> 
   </resources>

1 个答案:

答案 0 :(得分:0)

您可以使用另一个属性源并将其放置在目标文件夹中。

例如

@PropertySources({@PropertySource(value="file:somefolder/conf/bootstrap.properties", ignoreResourceNotFound=true), 
    @PropertySource(value="file:conf/bootstrap.properties", ignoreResourceNotFound=true)})

在服务器文件中保存在 somefolder / conf 中,然后在文件中定义加载属性。

@Value("${datasource.url}")
private String url;

@Value("${datasource.username}")
private String username;

@Value("${datasource.password}")
private String password;

一旦加载,便可以在任何地方使用。

放置

ignoreResourceNotFound = true 的原因是,如果找不到属性,则不会发生异常,这都是基于场景的。您可以改回false

相关问题