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
相关产品推荐
相关产品推荐

