最代码官方的gravatar头像
最代码官方 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/

项目截图(必填)

Spring Boot演示@ConfigurationProperties标注实现自定义配置属性的实例

运行截图(必填)

参数值验证失败时

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

Spring Boot演示@ConfigurationProperties标注实现自定义配置属性的实例

2.native2ascii.exe转换中文

app.site.name= \u6700\u4ee3\u7801
app.site.domain= www.zuidaima.com

Spring Boot演示@ConfigurationProperties标注实现自定义配置属性的实例

3.如果要读取yaml文件,可以删掉properties文件,修改dapplication.yml为application.yml即可

Spring Boot演示@ConfigurationProperties标注实现自定义配置属性的实例


打赏

文件名:configuration-properties.zip,文件大小:5.405K 下载
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友