先说这个低级错误: 配置类中忘记给属性添加get和set方法
原先的配置类代码
@Component
@ConfigurationProperties(prefix = "captcha")
public class CaptchaPropertiesConfig {
/**
* 图片验证码的过期时间,秒
*/
private Long expirationTime = 15000L;
}
我发现在编写yaml文件没有,输入captcha没有相应的提示
排查点
然后逐一排查了:
- 检查导入依赖,这里我也导入了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
- 配置类添加@Component和@ConfigurationProperties注解并指定prefix,这里我都添加了
@Component
@ConfigurationProperties(prefix = "captcha")
public class CaptchaPropertiesConfig {
/**
* 图片验证码的过期时间,秒
*/
private Long expirationTime = 15000L;
}
- 检查主启动类是否添加@EnableConfigurationProperties注解,这里我也添加了
@EnableConfigurationProperties(value = {CaptchaPropertiesConfig.class})
public class xxxxApplication {
public static void main(String[] args) {
SpringApplication.run(xxxxApplication.class, args);
}
}
- 重新build以及重启IDEA,都无果
低级错误点
后来发现,是配置类忘记写属性的get和set方法了,可以手动添加属性的get和set方法,或者使用lombok的话,在配置类上添加@Data注解,之后重新运行springboot应用就可以了
重新运行主程序之后,来到yaml配置文件中,就有了提示了
// lombok
@Component
@Data
@ConfigurationProperties(prefix = "captcha")
public class CaptchaPropertiesConfig {
/**
* 图片验证码的过期时间,秒
*/
private Long expirationTime = 15000L;
}
或者
// 手动添加get和set方法
@Component
@ConfigurationProperties(prefix = "captcha")
public class CaptchaPropertiesConfig {
/**
* 图片验证码的过期时间,秒
*/
private Long expirationTime = 15000L;
public Long getExpirationTime() {
return expirationTime;
}
public void setExpirationTime(Long expirationTime) {
this.expirationTime = expirationTime;
}
}
当然可以提示之后,idea依旧会提示:
Re-run Spring Boot Configuration Annotation Processor to update generated metadata
可以看出这个提示是不会影响什么的