You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven SpringBoot项目JAR无法读取application.properties问题求助

Spring Boot JAR运行时无法解析secure.url配置,但IDE直接运行正常

本地IDE直接运行主类时项目正常启动,但构建成JAR包运行时抛出配置解析错误:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'secure.url' in value "${secure.url}"

项目此前运行正常,近期突发该问题。


相关文件信息

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.1</version>
        <relativePath/>
    </parent>
    <groupId>com.petrasys</groupId>
    <artifactId>client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>clientServer</name>
    <description>Client backend for Petrasys</description>
    <properties>
        <java.version>21</java.version>
        <org.mapstruct.version>1.5.5.Final</org.mapstruct.version>
        <jsonwebtoken.version>0.12.3</jsonwebtoken.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-api</artifactId>
            <version>${jsonwebtoken.version}</version>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-impl</artifactId>
            <version>${jsonwebtoken.version}</version>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-jackson</artifactId>
            <version>${jsonwebtoken.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.influxdb</groupId>
            <artifactId>influxdb-client-java</artifactId>
            <version>6.7.0</version>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-websocket</artifactId>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.0.Final</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.12.1</version>
                <configuration>
                    <source>21</source>
                    <target>21</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.properties

jwt.token-expiration-seconds=1800
jwt.secret-key=somekeyValuesThatArethere
spring.application.name=clientServer
influxdb.url=http://localhost:8086
influxdb.token=aTokenValueThatWorks
influxdb.org=my-org
influxdb.bucket=signal

spring.data.mongodb.uri=mongodb://localhost:27017/client
spring.main.allow-bean-definition-overriding=true
logging.level.org.springframework.security=DEBUG
logging.level.org.springframework.security.web.SecurityFilterChain=DEBUG
#logging.level.com.petrasys.client.handler.LiveDataHandler=DEBUG
#logging.level.com.petrasys.client.handler.AlertHandler=DEBUG
client.locations.latitudes=28.644800,28.567190,28.474388,28.457523,28.644800
client.locations.longitudes=77.216721,77.320892,77.503990,77.026344,77.216721

sms.api.url=http://url.that.works/path
sms.port=/path/to/port
sms.baud_rate=657
secure.url=http://localhost:7573
pot.api=/pot/{pot_number}/{direction}
system.api=/config
system.settings.ladc=75
system.settings.lsys=500
system.settings.buzz=1

client.portal.url=http://localhost:3001/

cms.server.url=http://localhost:8081/


# Enable logging for MongoDB operations
#logging.level.org.springframework.data.mongodb.core.MongoTemplate=DEBUG
#logging.level.org.mongodb.driver=DEBUG

配置使用示例

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@Component
public class PotentiometerHandler {

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

    @Value("${pot.api}")
    private String potApi;

    @Value("${system.api}")
    private String systemApi;

    @Value("${system.settings.ladc}")
    private int ladc;

    @Value("${system.settings.lsys}")
    private int lsys;

    @Value("${system.settings.buzz}")
    private int buzz;

    private final WebClient webClient;

    public PotentiometerHandler(WebClient.Builder webClientBuilder) {
        this.webClient = webClientBuilder.baseUrl(secureApiUrl).build();
    }...

完整错误日志

10:39:26.095 [main] ERROR org.springframework.boot.SpringApplication -- Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'potentiometerHandler': Injection of autowired dependencies failed
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:514)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1418)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:325)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:975)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:960)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:625)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:464)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:334)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1358)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1347)
        at com.petrasys.client.PetrasysClientApplication.main(PetrasysClientApplication.java:10)
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'secure.url' in value "${secure.url}"
        at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:180)
        at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
        at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239)
        at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
        at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:200)
        at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:921)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1374)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1353)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:784)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:767)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:145)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:508)
        ... 16 common frames omitted

排查与解决步骤

1. 验证JAR包内的配置文件

解压JAR包检查配置是否被正确打包:

unzip client-0.0.1-SNAPSHOT.jar -d temp-check

进入temp-check/BOOT-INF/classes/目录,确认application.properties存在且包含secure.url配置。如果缺失,说明构建时资源未被打包。

2. 修复Maven资源打包配置

确保application.properties放在src/main/resources目录下,若pom.xml未明确资源配置,添加以下内容到<build>节点:

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
    </resource>
</resources>

3. 重新构建JAR

使用标准Spring Boot构建命令,确保资源被正确打包:

mvn clean install

4. 修正Bean初始化顺序

原代码在构造方法中直接使用字段注入的配置值,可能因初始化顺序导致值未被注入。改为构造方法注入所有依赖:

@Component
public class PotentiometerHandler {

    private final String secureApiUrl;
    private final String potApi;
    private final String systemApi;
    private final int ladc;
    private final int lsys;
    private final int buzz;
    private final WebClient webClient;

    public PotentiometerHandler(WebClient.Builder webClientBuilder,
                                @Value("${secure.url}") String secureApiUrl,
                                @Value("${pot.api}") String potApi,
                                @Value("${system.api}") String systemApi,
                                @Value("${system.settings.ladc}") int ladc,
                                @Value("${system.settings.lsys}") int lsys,
                                @Value("${system.settings.buzz}") int buzz) {
        this.secureApiUrl = secureApiUrl;
        this.potApi = potApi;
        this.systemApi = systemApi;
        this.ladc = ladc;
        this.lsys = lsys;
        this.buzz = buzz;
        this.webClient = webClientBuilder.baseUrl(secureApiUrl).build();
    }

    // 其他业务代码
}

5. 检查环境配置冲突

如果使用多环境配置,运行JAR时指定正确的环境:

java -jar client-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev

确保激活的配置文件包含secure.url项。


内容的提问来源于stack exchange,提问作者Maxsash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 19:38:09