在Spring boot项目中,采用@ConfigurationProperties(frefix = “config”)配置实体参数。
启动项目之后,会遇到Nacos配置忘记填,导致配置属性为空。(不像@value注解,不配置参数就会启动报错,可以立马发现配置没写)
那么ConfigurationProperties实体配置有没有办法在项目启动时,没填写就启动报错、校验参数是否已经在nacos填写完成呢? 恭喜你本文就是答案!!!
1. 通过Springboot的@Validated注解实现对象实体参数校验
对象实体类上加上@Validated注解
在属性上加上javax.validation.constraints包下面对应的参数校验注解
@NotNull:不能为null,但可以为empty
@NotEmpty:不能为null,而且长度必须大于0
@NotBlank:只能作用在String上,不能为null,而且调用trim()后,长度必须大于0
2. 普通实体类配置校验 普通的对象配置,举例在nacos中需要如下配置信息:
1 2 3 4 config: username: 张三 password: admin age: 20
那么对应的对象配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Configuration;import org.springframework.validation.annotation.Validated;import javax.validation.constraints.NotEmpty;import javax.validation.constraints.NotNull;@Data @Validated @Configuration @ConfigurationProperties(prefix = "config") public class ConfigProperties { @NotEmpty private String username; @NotEmpty private String password; @NotNull private String age; }
2.1 如果Nacos中未配置属性,启动报错信息如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2022-05-18 16:52:29 [main] INFO o.s.b.a.l.ConditionEvaluationReportLoggingListener -- Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2022-05-18 16:52:29 [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter -- *************************** APPLICATION FAILED TO START *************************** Description: Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'config' to config.properties.ConfigProperties$$EnhancerBySpringCGLIB$$3398961e failed: Property: config.username Value: null Reason: 不能为空 Property: config.age Value: null Reason: 不能为null Property: config.password Value: null Reason: 不能为空 Action: Update your application's configuration
2.2 获取属性,能动态刷新配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import org.springframework.stereotype.Service;import org.springframework.beans.factory.annotation.Autowired;@Service public class Demo { @Autowired private ConfigProperties config; public void getUser () { System.out.println(config.getUsername); System.out.println(config.getPassword); System.out.println(config.getAge); } }
2. 带内部类实体配置校验 1 2 3 4 5 6 7 8 9 10 11 12 13 config: username: 张三 password: admin age: 20 info: profession: 厨师 hobby: - 吃饭 - 睡觉 - 打豆豆 enterprise: company: 五星米其林总部 address: 火星
对应的Java对象配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 @Data @Validated @Configuration @ConfigurationProperties(prefix = "config") public class ConfigProperties { @NotEmpty private String username; @NotEmpty private String password; @NotNull private String age; @NotNull private Info info; @NotNull private Enterprise enterprise; @Data public static class Info { @NotEmpty private String profession; @NotNull private List<String> hobby; } @Data public static class Enterprise { @NotEmpty private String company; @NotEmpty private String address; } }
访问对象可以看到属性已经配置成功,并且有校验
1 ConfigProperties(username=张三, password=admin, age=20, info=ConfigProperties.Info(profession=厨师, hobby=[吃饭, 睡觉, 打豆豆]), enterprise=ConfigProperties.Enterprise(company=五星米其林总部, address=火星))
2.1 对应有校验注解的属性,不配置启动就会报错提示,对于不需要校验的配置不加注解即可,并且这种方式配置支持动态刷新