有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java找不到应用程序。属性文件从War文件的外面

在应用程序外部化的过程中。财产档案在战争之外。当我通过jenkins创建war文件时,显示错误

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2019-11-28 18:06:07.900 ERROR 22612 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :


APPLICATION FAILED TO START


Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

Action:

Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath. If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

2019-11-28 18:06:07.909 ERROR 22612 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@233795b6] to prepare test instance [com.assessmentAggregator.api.AssessmentAggregatorApiApplicationTests@74024f3]

java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125) ~[spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108) ~[spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190) ~[spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132) ~[spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246) ~[spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) [spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) [spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) [spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246) [spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) [spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12] at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12] at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12] at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12] at 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) 个答案

  1. # 1 楼答案

    您可以使用放置在目标文件夹中的其他属性源和点

    比如

    @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