最代码官方
2020-12-20 17:23:16
证
Spring Boot演示@ConfigurationProperties标注实现自定义配置属性的实例
项目描述
spring支持自定义properties或yaml格式的配置文件,这个实例演示如何自定义配置属性,并且可以实现参数值的验证
运行环境
jdk8+mysql+IntelliJ IDEA+maven
项目技术(必填)
spring+spring boot+ConfigurationProperties
数据库文件(可选)
无
依赖包文件(可选)
maven搭建
链接:https://pan.baidu.com/s/1csAQasq3JPMcxUP5k8gbhw
提取码:vkrn
是否原创(转载必填原文地址)
转载https://memorynotfound.com/spring-boot-configurationproperties-annotation-example/
项目截图(必填)
运行截图(必填)
参数值验证失败时
Description: Binding to target ApplicationProperties{cache=Cache{ttl=86400, maxEntries=1000}, cors=Cors{allowedOrigins=[*], allowedMethods=[GET, PUT, POST, DELETE, OPTIONS], allowedHeaders=[*], allowCredentials=true, maxAge=3600}} failed: Property: app.cache.ttl Value: 86400 Reason: 最大不能超过1000 Property: app.site Value: null Reason: 不能为null Action: Update your application's configuration
参数值校验成功时
"D:\Program Files\Java\jdk1.8.0_171\bin\java.exe" -Dfile.encoding=UTF-8 -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2020.2.1\lib\idea_rt.jar=49935:C:\Program Files\JetBrains\IntelliJ IDEA 2020.2.1\bin" -classpath "D:\Program Files\Java\jdk1.8.0_171\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_171\jre\lib\rt.jar;D:\workspace-idea\memorynotfound\spring-framework\spring-boot\configuration-properties\target\classes;D:\apache-maven-3.5.4\.m2\repository\org\springframework\boot\spring-boot-starter\1.5.3.RELEASE\spring-boot-starter-1.5.3.RELEASE.jar;D:\apache-maven-3.5.4\.m2\repository\org\springframework\boot\spring-boot\1.5.3.RELEASE\spring-boot-1.5.3.RELEASE.jar;D:\apache-maven-3.5.4\.m2\repository\org\springframework\spring-context\4.3.8.RELEASE\spring-context-4.3.8.RELEASE.jar;D:\apache-maven-3.5.4\.m2\repository\org\springframework\spring-aop\4.3.8.RELEASE\spring-aop-4.3.8.RELEASE.jar;D:\apache-maven-3.5.4\.m2\repository\org\springframework\spring-beans\4.3.8.RELEASE\spring-beans-4.3.8.RELEASE.jar;D:\apache-maven-3.5.4\.m2\repository\org\springframework\spring-expression\4.3.8.RELEASE\spring-expression-4.3.8.RELEASE.jar;D:\apache-maven-3.5.4\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\1.5.3.RELEASE\spring-boot-autoconfigure-1.5.3.RELEASE.jar;D:\apache-maven-3.5.4\.m2\repository\org\springframework\boot\spring-boot-starter-logging\1.5.3.RELEASE\spring-boot-starter-logging-1.5.3.RELEASE.jar;D:\apache-maven-3.5.4\.m2\repository\ch\qos\logback\logback-classic\1.1.11\logback-classic-1.1.11.jar;D:\apache-maven-3.5.4\.m2\repository\ch\qos\logback\logback-core\1.1.11\logback-core-1.1.11.jar;D:\apache-maven-3.5.4\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;D:\apache-maven-3.5.4\.m2\repository\org\slf4j\jcl-over-slf4j\1.7.25\jcl-over-slf4j-1.7.25.jar;D:\apache-maven-3.5.4\.m2\repository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;D:\apache-maven-3.5.4\.m2\repository\org\slf4j\log4j-over-slf4j\1.7.25\log4j-over-slf4j-1.7.25.jar;D:\apache-maven-3.5.4\.m2\repository\org\springframework\spring-core\4.3.8.RELEASE\spring-core-4.3.8.RELEASE.jar;D:\apache-maven-3.5.4\.m2\repository\org\yaml\snakeyaml\1.17\snakeyaml-1.17.jar;D:\apache-maven-3.5.4\.m2\repository\org\hibernate\hibernate-validator\5.3.5.Final\hibernate-validator-5.3.5.Final.jar;D:\apache-maven-3.5.4\.m2\repository\javax\validation\validation-api\1.1.0.Final\validation-api-1.1.0.Final.jar;D:\apache-maven-3.5.4\.m2\repository\org\jboss\logging\jboss-logging\3.3.1.Final\jboss-logging-3.3.1.Final.jar;D:\apache-maven-3.5.4\.m2\repository\com\fasterxml\classmate\1.3.3\classmate-1.3.3.jar" com.memorynotfound.springboot.Application . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.3.RELEASE) 2020-12-20 17:21:20.421 INFO 2300 --- [ main] c.memorynotfound.springboot.Application : Starting Application on CIFX5T2TRH4ZE3H with PID 2300 (D:\workspace-idea\memorynotfound\spring-framework\spring-boot\configuration-properties\target\classes started by Administrator in D:\workspace-idea\memorynotfound) 2020-12-20 17:21:20.426 INFO 2300 --- [ main] c.memorynotfound.springboot.Application : No active profile set, falling back to default profiles: default 2020-12-20 17:21:20.487 INFO 2300 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5cdd8682: startup date [Sun Dec 20 17:21:20 CST 2020]; root of context hierarchy 2020-12-20 17:21:21.548 WARN 2300 --- [ main] o.h.v.m.ParameterMessageInterpolator : HV000184: ParameterMessageInterpolator has been chosen, EL interpolation will not be supported 2020-12-20 17:21:21.902 WARN 2300 --- [ main] o.h.v.m.ParameterMessageInterpolator : HV000184: ParameterMessageInterpolator has been chosen, EL interpolation will not be supported 2020-12-20 17:21:22.242 INFO 2300 --- [ main] c.memorynotfound.springboot.Application : Spring Boot - @ConfigurationProperties example 2020-12-20 17:21:22.279 INFO 2300 --- [ main] c.memorynotfound.springboot.Application : ApplicationProperties{cache=Cache{ttl=1000, maxEntries=3600}, cors=Cors{allowedOrigins=[*], allowedMethods=[GET, PUT, POST, DELETE, OPTIONS], allowedHeaders=[*], allowCredentials=true, maxAge=3600},site:Site{name=最代码,domain:www.zuidaima.com}} 2020-12-20 17:21:22.653 INFO 2300 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2020-12-20 17:21:22.685 INFO 2300 --- [ main] c.memorynotfound.springboot.Application : Started Application in 2.686 seconds (JVM running for 5.075) 2020-12-20 17:21:22.687 INFO 2300 --- [ Thread-7] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5cdd8682: startup date [Sun Dec 20 17:21:20 CST 2020]; root of context hierarchy 2020-12-20 17:21:22.691 INFO 2300 --- [ Thread-7] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown Process finished with exit code 0
注意事项(可选)
1.properties文件有中文有乱码时,设置文件编码
-Dfile.encoding=UTF-8
2.native2ascii.exe转换中文
app.site.name= \u6700\u4ee3\u7801 app.site.domain= www.zuidaima.com
3.如果要读取yaml文件,可以删掉properties文件,修改dapplication.yml为application.yml即可
猜你喜欢
- Spring Boot配置@Profile注解加载不同环境的配置文件实例
- Spring Boot学习(二)之属性配置文件详解 博客代码
- Spring Boot创建自定义Banner.txt实例
- Spring Boot整合VueJS+spring data jpa实现RESTful API实例
- Spring Boot整合Ehcache的简单入门实例
- spring AOP 过滤器 拦截器 执行顺序示例
- springboot2.x+mybatis-plus+jsp+jpa简单增删改查实例(仅供学习)
- Spring Boot Actuator 2.3.4.RELEASE新版本实现自定义端点信息的配置实例
- Swagger的简单案例,适合初级者学习使用
- spring boot使用不指定Maven parent pom来创建可执行的spring boot项目
- spring boot+mybatis开发java web网页版电子商城网站(适合spring boot初学者学习)
- SpringBoot入门整合开发java Web项目实例
请下载代码后再发表评论
文件名:configuration-properties.zip,文件大小:5.405K
下载
- /
- /configuration-properties
- /configuration-properties/pom.xml
- /configuration-properties/src
- /configuration-properties/src/main
- /configuration-properties/src/main/java
- /configuration-properties/src/main/java/com
- /configuration-properties/src/main/java/com/memorynotfound
- /configuration-properties/src/main/java/com/memorynotfound/springboot
- /configuration-properties/src/main/java/com/memorynotfound
- /configuration-properties/src/main/java/com
- /configuration-properties/src/main/resources
- /configuration-properties/src/main/resources/application.properties
- /configuration-properties/src/main/java
- /configuration-properties/src/main
- /configuration-properties

- 证 Spring Boot创建自定义Banner.txt实例
- 原证 Spring Boot整合VueJS+spring data jpa实现RESTful API实例
- 证 Spring Boot整合Ehcache的简单入门实例
- 原证 spring AOP 过滤器 拦截器 执行顺序示例
- 证 Spring Boot配置@Profile注解加载不同环境的配置文件实例
- 原证 springboot2.x+mybatis-plus+jsp+jpa简单增删改查实例(仅供学习)
- 证 Spring Boot Actuator 2.3.4.RELEASE新版本实现自定义端点信息的配置实例
- 原证 Swagger的简单案例,适合初级者学习使用
- 证 spring boot使用不指定Maven parent pom来创建可执行的spring boot项目
- 证精 spring boot+mybatis开发java web网页版电子商城网站(适合spring boot初学者学习)
- 原 SpringBoot入门整合开发java Web项目实例
- 证 Spring Boot整合VueJS+spring data jpa实现待办事项TODO项目实例

最代码官方 LV166
2020年12月20日

zsf0061 LV8
3月14日
hzhsh0223 LV18
2月3日
yohohero LV1
1月14日
lbnlbn
2022年12月26日
暂无贡献等级
lewiszz LV2
2022年12月11日
329512801 LV27
2021年6月23日
lbwnblbwnblbwnb LV7
2021年6月21日
别碰我被窝 LV8
2021年6月1日
skycrawler LV6
2021年5月28日
15195859092 LV2
2021年5月24日