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

Spring Boot无法从application.properties读取属性问题求助

Spring Boot @Value读取属性失败导致Bean创建异常

我有一个小型Spring Boot应用,尝试从application.properties中读取属性到@Component类,但出现Bean创建失败错误,提示“0是无效的周期令牌值,令牌必须为正数”。控制台输出显示throttleRate和refillDuration均为0,既未读取到配置文件值,也未使用注解中指定的默认值。

组件代码

@Component
public class Bucket4jRateLimiterV2 {

    @Value("${ratelimit:100}")
    private int throttleRate;

    @Value("${thrrotleDuration:1}")
    private int refillDuration;

    private Refill refill;

    private Bandwidth limit;

    private Bucket BUCKET;

    public Bucket4jRateLimiterV2() {
        System.out.println("Values are : " + throttleRate + " " + refillDuration);
        refill = Refill.intervally(throttleRate, Duration.ofMinutes(refillDuration));
        limit = Bandwidth.classic(5, refill);
        BUCKET = Bucket4j.builder().addLimit(limit).build();
    }

    public boolean isAPICallAllowed() {
        System.out.println("thrrotleRate : " + throttleRate);
        System.out.println("Available tokens are  : " + BUCKET.getAvailableTokens() + " called from thread : "
                + Thread.currentThread().getName());
        return BUCKET.tryConsume(TOKEN_COUNT);
    }
}

application.properties配置

ratelimit = 500
throttleDuration = 1

主类注解

@EnableAsync
@SpringBootApplication
@ComponentScan(basePackages = "com.nishit")
@EnableCaching(proxyTargetClass = true)

已尝试的操作

  • 验证application.properties已在类路径中;
  • 更换@Component、@Service、@Controller注解尝试读取属性。

请问我哪里操作有误?


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 18:39:41