最代码官方
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即可
猜你喜欢
请下载代码后再发表评论
文件名: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
相关代码
- springboot demo小例子
- 原 SpringBoot入门整合开发java Web项目实例
- 证精 Springboot开发的精简博客系统
- 证 Springboot实现的图书商城系统
- 原证 springboot麦克风录制声音文件保存本地,可拓展ASR识别后返回
- 证精 SpringBoot开发非常美观的java博客系统(包含后台管理功能)
- 原 springboot入门之hello world(小白必看)
- 证精 Springboot开发的客户关系管理系统SpringbootCRM
- 原证 springboot通过qq邮箱发送邮件
- 证 SpringBoot用户登录注册简单实例
- 原 springboot入门demo实例
- 证 SpringBoot整合MyBatis实现记录孩子成长过程的博客系统
最近下载
最代码官方 LV168
2020年12月20日
最近浏览
444105047 LV6
2023年7月28日
微信网友_6522007290040320 LV2
2023年6月16日
zsf0061 LV8
2023年3月14日
hzhsh0223 LV18
2023年2月3日
yohohero LV1
2023年1月14日
lbnlbn
2022年12月26日
暂无贡献等级
lewiszz LV2
2022年12月11日
329512801 LV28
2021年6月23日
lbwnblbwnblbwnb LV7
2021年6月21日
别碰我被窝 LV9
2021年6月1日