spring boot could not resolve placeholder in string value 问题解决方法

本文介绍了解决 Spring Boot 应用中因占位符未解析而导致的 IllegalArgumentException 异常的方法,包括配置 PropertySourcesPlaceholderConfigurer 和为属性名添加统一前缀。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'hosts' in string value "${db.hosts}"


问题的产生是由于有多个properties文件造成的,如果再第一个properties文件中没有找,就不认为没有了,不继续找下一个properties文件


解决办法如下:

方法 一、

在Application中加入一个静态方法


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;


@Configuration
@SpringBootApplication
@ComponentScan
public class TestApplication {


public final static void main(String[] args) {
SpringApplication.run(VfcadaptorApplication.class, args);
}

@Bean
    public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
        PropertySourcesPlaceholderConfigurer c = new PropertySourcesPlaceholderConfigurer();
        c.setIgnoreUnresolvablePlaceholders(true);
        return c;
    }
}


方法二、 统一为属性name加前缀

<span lang="EN-US" font-size:11.5pt;font-family:consolas;"="">app.datasource.foo.type=daffaDataSource
app.datasource.foo.status =30

那就需要在类文件上加注解 

@ConfigurationProperties("app.datasource.foo")

@ConfigurationProperties("app.datasource.foo")

Publicclass AA{

PrivateString type;

PrivateString status;

...

}


<think>我们正在解决Spring Boot应用中无法解析配置占位符'spring.boot.admin.client.username'的问题。这个问题通常发生在Spring Boot尝试注入一个配置属性,但该属性在配置源中找不到时。 可能的原因和解决方法如下: 1. **检查属性名称是否正确**:确保在配置文件中使用的属性名称与代码中引用的名称完全一致,包括大小写和分隔符(通常使用`.`或`-`,但Spring Boot会将其标准化)。例如,在`application.properties`中应该有这样的条目: ```properties spring.boot.admin.client.username=admin ``` 2. **检查配置文件的位置和加载顺序**:确保包含该属性的配置文件(如`application.properties`或`application.yml`)位于正确的位置(如`src/main/resources`),并且没有被其他配置文件覆盖。 3. **检查多环境配置**:如果使用profile(如`application-dev.properties`),请确保在激活的profile中定义了该属性。 4. **检查属性前缀**:注意`spring.boot.admin.client`是属性前缀,后面的`username`是属性名。整个属性键是`spring.boot.admin.client.username`。 5. **使用@Value注解时的默认值**:如果在代码中使用`@Value`注解,可以设置默认值以避免找不到属性时出错: ```java @Value("${spring.boot.admin.client.username:defaultUser}") private String username; ``` 这样,当属性不存在时,会使用`defaultUser`作为默认值。 6. **检查依赖和自动配置**:确保项目中包含了Spring Boot Admin Client的依赖。在Maven项目中,需要添加如下依赖: ```xml <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>你的版本</version> </dependency> ``` 7. **检查配置属性源**:如果属性是在自定义的属性源(如数据库、自定义文件)中,确保该属性源被正确加载。 8. **查看完整的配置属性**:可以通过启动应用时添加`--debug`参数来查看所有绑定的配置属性,从而确认该属性是否被正确加载。 9. **检查属性占位符的解析时机**:如果属性在Bean创建之后才可用,可能需要使用`@ConfigurationProperties`来绑定属性,而不是`@Value`。因为`@ConfigurationProperties`在Bean初始化之后处理,而`@Value`是在初始化之前。 10. **检查日志**:启动应用时,查看日志中是否有关于属性占位符无法解析的警告或错误信息,这有助于定位问题。 如果以上步骤仍无法解决问题,请提供更多的上下文信息,例如相关的配置文件和代码片段。 相关问题: 1. Spring Boot中如何管理多环境配置? 2. 如何使用@ConfigurationProperties绑定配置属性? 3. Spring Boot Admin Client的常见配置有哪些?</think>### Spring Boot 解析占位符 'spring.boot.admin.client.username' 错误解决方案 #### 问题原因分析 该错误通常发生在以下场景: 1. **配置缺失**:在 `application.properties` 或 `application.yml` 中未定义 `spring.boot.admin.client.username` 属性 2. **依赖缺失**:未添加 Spring Boot Admin Client 依赖 3. **占位符语法错误**:属性引用格式不正确 4. **配置加载顺序问题**:属性在 Bean 初始化之后才加载 #### 解决步骤 1. **添加必要依赖** (pom.xml) ```xml <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.7.4</version> <!-- 使用最新版本 --> </dependency> ``` 2. **配置属性文件** (application.yml) ```yaml spring: boot: admin: client: url: http://your-admin-server:8080 username: admin123 # 必须配置此属性 password: securePass ``` 3. **检查属性引用** (Java 配置类) ```java @Value("${spring.boot.admin.client.username}") // 确保引用格式正确 private String adminUsername; ``` 4. **添加默认值** (防止属性缺失) ```java @Value("${spring.boot.admin.client.username:defaultAdmin}") private String adminUser; ``` 5. **验证配置加载**: 启动时添加 VM 参数: ```bash java -jar your-app.jar --debug ``` 在日志中搜索: ```log Properties with 'spring.boot.admin.client' prefix ``` #### 常见错误排查 1. 检查属性名拼写:确保使用全小写+`.`分隔符格式 2. 确认配置文件位置:`src/main/resources/application.yml` 3. 多环境配置:检查 `application-{profile}.yml` 中是否覆盖了属性 4. 版本兼容性:Spring Boot Admin Client 版本需与 Spring Boot 版本匹配 > **重要提示**:如果使用 Spring Cloud Config,需确保配置中心包含此属性,否则会出现 `Could not resolve placeholder` 错误[^1] #### 预防措施 ```java @Configuration @EnableConfigurationProperties public class AdminConfig { @Bean @ConfigurationProperties("spring.boot.admin.client") public AdminClientProperties adminClientProperties() { return new AdminClientProperties(); // 类型安全绑定 } } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值