Spring Boot应用程序未在

时间:2018-12-04 15:18:08

标签: java spring maven spring-boot

我正在使用Spring Boot应用程序,正在尝试启用外部application.properties,以便在从控制台运行jar时可以注入这些属性。

我的Configuration类中有此注释     @PropertySource(value =“ file:$ {env.properties.location} /application.properties”)

其余的配置类:

@Component
@Configuration
@EnableCassandraRepositories(basePackages = "uk.co.dashboard.repository")
@PropertySource(value="file:${env.properties.location}/application.properties")
public class CassandraConfiguration {

@Value("${cassandra.contactpoints}")
private String[] cassandraContactPoint;

@Value("${cassandra.port}")
private Integer cassandraPort;

@Value("${cassandra.keyspace}")
private String keyspaceName;



@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

@Bean
public MappingManager mappingManager(@Qualifier("session") Session session) {
    return new MappingManager(session);
}

@Bean
public Session session() {
    Cluster cluster = connectToCluster();
    Session session = cluster.connect(keyspaceName);
    return session;
}

@Bean
public DataRepository dataRepository(MappingManager mappingManager){
    return mappingManager.createAccessor(DataRepository.class);
}


public Cluster connectToCluster() {
    QueryOptions queryOptions = new QueryOptions();
    queryOptions.setConsistencyLevel(ConsistencyLevel.QUORUM);
    Cluster cluster = Cluster.builder()
            .addContactPoints(cassandraContactPoint)
            .withPort(cassandraPort)
            .withQueryOptions(queryOptions)
            .build();
    return cluster;
}

protected String getKeyspaceName() {
    return keyspaceName;
}

@Bean
public BasedataService basedataService(){
    return new BasedataService();
}

我正在终端中运行此命令

java -jar basedata-dashboard-1.0.0-SNAPSHOT.jar -Denv.properties.location ='/ home / Desktop'

在我的桌面上,是一个名为application.properties的文件,其中包含配置详细信息。但是,当运行上面的命令时,我得到了:

2018-12-04 14:48:09.103 ERROR 9964 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [uk.co.dashboard.Application]; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'env.properties.location' in value "file:${env.properties.location}/application.properties"
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:184) ~[spring-context-5.0.11.RELEASE.jar!/:5.0.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:316) ~[spring-context-5.0.11.RELEASE.jar!/:5.0.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:233) ~[spring-context-5.0.11.RELEASE.jar!/:5.0.11.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:271) ~[spring-context-5.0.11.RELEASE.jar!/:5.0.11.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:91) ~[spring-context-5.0.11.RELEASE.jar!/:5.0.11.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:692) ~[spring-context-5.0.11.RELEASE.jar!/:5.0.11.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:530) ~[spring-context-5.0.11.RELEASE.jar!/:5.0.11.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.0.7.RELEASE.jar!/:2.0.7.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) [spring-boot-2.0.7.RELEASE.jar!/:2.0.7.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) [spring-boot-2.0.7.RELEASE.jar!/:2.0.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-2.0.7.RELEASE.jar!/:2.0.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242) [spring-boot-2.0.7.RELEASE.jar!/:2.0.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230) [spring-boot-2.0.7.RELEASE.jar!/:2.0.7.RELEASE]
at uk.co.hermes.hubsanddepots.basedata.dashboard.Application.main(Application.java:11) [classes!/:1.0.0-SNAPSHOT]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_191]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_191]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_191]
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [basedata-dashboard-1.0.0-SNAPSHOT.jar:1.0.0-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [basedata-dashboard-1.0.0-SNAPSHOT.jar:1.0.0-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [basedata-dashboard-1.0.0-SNAPSHOT.jar:1.0.0-SNAPSHOT]
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [basedata-dashboard-1.0.0-SNAPSHOT.jar:1.0.0-SNAPSHOT]
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder     'env.properties.location' in value "file:${env.properties.location}/application.properties"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:172) ~[spring-core-5.0.11.RELEASE.jar!/:5.0.11.RELEASE]
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124) ~[spring-core-5.0.11.RELEASE.jar!/:5.0.11.RELEASE]
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:237) ~[spring-core-5.0.11.RELEASE.jar!/:5.0.11.RELEASE]
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:211) ~[spring-core-5.0.11.RELEASE.jar!/:5.0.11.RELEASE]
at org.springframework.core.env.AbstractEnvironment.resolveRequiredPlaceholders(AbstractEnvironment.java:571) ~[spring-core-5.0.11.RELEASE.jar!/:5.0.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processPropertySource(ConfigurationClassParser.java:451) ~[spring-context-5.0.11.RELEASE.jar!/:5.0.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:272) ~[spring-context-5.0.11.RELEASE.jar!/:5.0.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) ~[spring-context-5.0.11.RELEASE.jar!/:5.0.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:194) ~[spring-context-5.0.11.RELEASE.jar!/:5.0.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:296) ~[spring-context-5.0.11.RELEASE.jar!/:5.0.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) ~[spring-context-5.0.11.RELEASE.jar!/:5.0.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:202) ~[spring-context-5.0.11.RELEASE.jar!/:5.0.11.RELEASE]
at   org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:170) ~[spring-context-5.0.11.RELEASE.jar!/:5.0.11.RELEASE]

现在,当我在工作目录中有一个application.properties的本地副本时,我的应用程序就可以工作,但是出于意图的考虑,这是不可行的,我需要能够通过控制台对应用程序执行ping操作,并在出现以下情况时提供application.properties文件我这样做是因为这些变量将发生变化和/或应用将用于不同的目的。

我一直在网上寻找很多时间,但是我确实很困惑,我觉得我读过的大多数东西都告诉我它设置正确,但是我不知道为什么它不起作用

我要包含的最后一件事是pom:

<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>uk.co.dashboard.</groupId>
<artifactIddashboard</artifactId>
<version>1.0.0-SNAPSHOT</version>

<name>Dashboard</name>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.7.RELEASE</version>
</parent>


<dependencies>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.6.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery</artifactId>
        <version>3.2.1</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring4</artifactId>
        <version>2.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-cassandra</artifactId>
        <version>2.1.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.datastax.cassandra</groupId>
        <artifactId>cassandra-driver-mapping</artifactId>
        <version>2.1.6</version>
    </dependency>
    <dependency>
        <groupId>uk.co.hermes</groupId>
        <artifactId>basedata-cassandra-entities</artifactId>
        <version>1.21</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.22</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.cassandraunit</groupId>
        <artifactId>cassandra-unit</artifactId>
        <version>1.0.1.4</version>
        <scope>test</scope>
    </dependency>

</dependencies>
<build>

<plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>springloaded</artifactId>
                <version>1.2.8.RELEASE</version>
            </dependency>
        </dependencies>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
            <configuration>
                <!--<classifier>jwd</classifier>-->
                <!--<finalName>markiv</finalName>-->
                <mainClass>uk.co.hermes.hubsanddepots.basedata.dashboard.Application</mainClass>
            </configuration>
        </execution>
    </executions>
    </plugin>
</plugins>

</build>

1 个答案:

答案 0 :(得分:0)

在-jar选项之前提供JAVA_OPTS时,java的命令行参数运行良好。

我最初遇到过类似的问题,并且能够通过在-jar选项之前更改系统属性来解决此问题。

请尝试以下命令行选项:

java -Denv.properties.location='/home/Desktop' -jar basedata-dashboard-1.0.0-SNAPSHOT.jar