https://blog.csdn.net/wangmx1993328/article/details/81002901

  1. Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
  2. 2019-01-29 00:14:04.856 ERROR 10988 --- [ main] o.s.boot.SpringApplication : Application startup failed
  3.  
  4. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authorSettings': Could not bind properties to AuthorSettings (prefix=info.author, ignoreInvalidFields=false, ignoreUnknownFields=true, ignoreNestedProperties=false); nested exception is javax.validation.ValidationException: HV000029: Constraint factory returned null when trying to create instance of org.hibernate.validator.internal.constraintvalidators.bv.MaxValidatorForNumber.
  5. at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:336) ~[spring-boot-1.5.18.RELEASE.jar:1.5.18.RELEASE]
  6. at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:292) ~[spring-boot-1.5.18.RELEASE.jar:1.5.18.RELEASE]
  7. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:407) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  8. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1623) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  9. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  10. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  11. at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  12. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  13. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  14. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  15. at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  16. at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  17. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  18. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) ~[spring-boot-1.5.18.RELEASE.jar:1.5.18.RELEASE]
  19. at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) ~[spring-boot-1.5.18.RELEASE.jar:1.5.18.RELEASE]
  20. at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) ~[spring-boot-1.5.18.RELEASE.jar:1.5.18.RELEASE]
  21. at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:121) [spring-boot-test-1.5.18.RELEASE.jar:1.5.18.RELEASE]
  22. at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) [spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  23. at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) [spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  24. at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) [spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  25. at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189) [spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  26. at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131) [spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  27. at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) [spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  28. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) [spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  29. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) [spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  30. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]
  31. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289) [spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  32. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) [spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  33. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) [spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  34. at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]
  35. at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]
  36. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]
  37. at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]
  38. at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]
  39. at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  40. at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  41. at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
  42. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) [spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  43. at org.junit.runner.JUnitCore.run(JUnitCore.java:137) [junit-4.12.jar:4.12]
  44. at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) [junit-rt.jar:na]
  45. at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) [junit-rt.jar:na]
  46. at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) [junit-rt.jar:na]
  47. at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) [junit-rt.jar:na]
  48. Caused by: javax.validation.ValidationException: HV000029: Constraint factory returned null when trying to create instance of org.hibernate.validator.internal.constraintvalidators.bv.MaxValidatorForNumber.
  49. at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManager.createAndInitializeValidator(ConstraintValidatorManager.java:181) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  50. at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManager.getInitializedValidator(ConstraintValidatorManager.java:122) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  51. at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorNoUnwrapping(ConstraintTree.java:303) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  52. at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorInstanceForAutomaticUnwrapping(ConstraintTree.java:244) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  53. at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getInitializedConstraintValidator(ConstraintTree.java:163) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  54. at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:116) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  55. at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:87) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  56. at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:73) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  57. at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:621) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  58. at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:584) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  59. at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForSingleDefaultGroupElement(ValidatorImpl.java:528) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  60. at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:496) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  61. at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:461) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  62. at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:411) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  63. at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:208) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  64. at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:103) ~[spring-context-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  65. at org.springframework.validation.DataBinder.validate(DataBinder.java:878) ~[spring-context-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  66. at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:289) ~[spring-boot-1.5.18.RELEASE.jar:1.5.18.RELEASE]
  67. at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:250) ~[spring-boot-1.5.18.RELEASE.jar:1.5.18.RELEASE]
  68. at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:331) ~[spring-boot-1.5.18.RELEASE.jar:1.5.18.RELEASE]
  69. ... 42 common frames omitted
  70.  
  71. 2019-01-29 00:14:04.865 ERROR 10988 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@37374a5e] to prepare test instance [com.tangcheng.learning.custom.AuthorSettingsTest@14b0b7f9]
  72.  
  73. java.lang.IllegalStateException: Failed to load ApplicationContext
  74. at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) ~[spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  75. at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) ~[spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  76. at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189) ~[spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  77. at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131) ~[spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  78. at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) ~[spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  79. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) [spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  80. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) [spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  81. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]
  82. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289) [spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  83. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) [spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  84. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) [spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  85. at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]
  86. at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]
  87. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]
  88. at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]
  89. at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]
  90. at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  91. at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  92. at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
  93. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) [spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  94. at org.junit.runner.JUnitCore.run(JUnitCore.java:137) [junit-4.12.jar:4.12]
  95. at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) [junit-rt.jar:na]
  96. at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) [junit-rt.jar:na]
  97. at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) [junit-rt.jar:na]
  98. at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) [junit-rt.jar:na]
  99. Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authorSettings': Could not bind properties to AuthorSettings (prefix=info.author, ignoreInvalidFields=false, ignoreUnknownFields=true, ignoreNestedProperties=false); nested exception is javax.validation.ValidationException: HV000029: Constraint factory returned null when trying to create instance of org.hibernate.validator.internal.constraintvalidators.bv.MaxValidatorForNumber.
  100. at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:336) ~[spring-boot-1.5.18.RELEASE.jar:1.5.18.RELEASE]
  101. at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:292) ~[spring-boot-1.5.18.RELEASE.jar:1.5.18.RELEASE]
  102. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:407) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  103. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1623) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  104. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  105. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  106. at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  107. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  108. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  109. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  110. at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  111. at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  112. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  113. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) ~[spring-boot-1.5.18.RELEASE.jar:1.5.18.RELEASE]
  114. at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) ~[spring-boot-1.5.18.RELEASE.jar:1.5.18.RELEASE]
  115. at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) ~[spring-boot-1.5.18.RELEASE.jar:1.5.18.RELEASE]
  116. at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:121) ~[spring-boot-test-1.5.18.RELEASE.jar:1.5.18.RELEASE]
  117. at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) ~[spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  118. at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) ~[spring-test-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  119. ... 24 common frames omitted
  120. Caused by: javax.validation.ValidationException: HV000029: Constraint factory returned null when trying to create instance of org.hibernate.validator.internal.constraintvalidators.bv.MaxValidatorForNumber.
  121. at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManager.createAndInitializeValidator(ConstraintValidatorManager.java:181) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  122. at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManager.getInitializedValidator(ConstraintValidatorManager.java:122) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  123. at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorNoUnwrapping(ConstraintTree.java:303) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  124. at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorInstanceForAutomaticUnwrapping(ConstraintTree.java:244) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  125. at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getInitializedConstraintValidator(ConstraintTree.java:163) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  126. at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:116) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  127. at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:87) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  128. at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:73) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  129. at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:621) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  130. at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:584) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  131. at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForSingleDefaultGroupElement(ValidatorImpl.java:528) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  132. at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:496) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  133. at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:461) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  134. at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:411) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  135. at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:208) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
  136. at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:103) ~[spring-context-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  137. at org.springframework.validation.DataBinder.validate(DataBinder.java:878) ~[spring-context-4.3.21.RELEASE.jar:4.3.21.RELEASE]
  138. at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:289) ~[spring-boot-1.5.18.RELEASE.jar:1.5.18.RELEASE]
  139. at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:250) ~[spring-boot-1.5.18.RELEASE.jar:1.5.18.RELEASE]
  140. at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:331) ~[spring-boot-1.5.18.RELEASE.jar:1.5.18.RELEASE]
  141. ... 42 common frames omitted
  142.  
  143. java.lang.IllegalStateException: Failed to load ApplicationContext
  144.  
  145. at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
  146. at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
  147. at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189)
  148. at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131)
  149. at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
  150. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
  151. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
  152. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  153. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
  154. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
  155. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
  156. at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  157. at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  158. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  159. at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  160. at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  161. at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
  162. at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
  163. at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  164. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
  165. at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  166. at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
  167. at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
  168. at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
  169. at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  170. Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authorSettings': Could not bind properties to AuthorSettings (prefix=info.author, ignoreInvalidFields=false, ignoreUnknownFields=true, ignoreNestedProperties=false); nested exception is javax.validation.ValidationException: HV000029: Constraint factory returned null when trying to create instance of org.hibernate.validator.internal.constraintvalidators.bv.MaxValidatorForNumber.
  171. at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:336)
  172. at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:292)
  173. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:407)
  174. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1623)
  175. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
  176. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481)
  177. at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
  178. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
  179. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
  180. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
  181. at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
  182. at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
  183. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
  184. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
  185. at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
  186. at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
  187. at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:121)
  188. at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
  189. at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
  190. ... 24 more
  191. Caused by: javax.validation.ValidationException: HV000029: Constraint factory returned null when trying to create instance of org.hibernate.validator.internal.constraintvalidators.bv.MaxValidatorForNumber.
  192. at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManager.createAndInitializeValidator(ConstraintValidatorManager.java:181)
  193. at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManager.getInitializedValidator(ConstraintValidatorManager.java:122)
  194. at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorNoUnwrapping(ConstraintTree.java:303)
  195. at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorInstanceForAutomaticUnwrapping(ConstraintTree.java:244)
  196. at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getInitializedConstraintValidator(ConstraintTree.java:163)
  197. at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:116)
  198. at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:87)
  199. at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:73)
  200. at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:621)
  201. at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:584)
  202. at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForSingleDefaultGroupElement(ValidatorImpl.java:528)
  203. at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:496)
  204. at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:461)
  205. at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:411)
  206. at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:208)
  207. at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:103)
  208. at org.springframework.validation.DataBinder.validate(DataBinder.java:878)
  209. at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:289)
  210. at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:250)
  211. at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:331)
  212. ... 42 more
  213.  
  214. Process finished with exit code -1

@ConfigurationProperties和@value都是将外部属性注入到对象

@ConfigurationProperties很方便使用。 比用@value注解好吗? 在特定的方案中是的,这只是一个选择问题

@EnableConfigurationProperties //开启属性注入,有此注解就可以通过@autowired注入, 是配合@ConfigurationProperties使用的。
如果没有@EnableConfigurationProperties,则使用@ConfigurationProperties注解的class上面还需要添加@Component(@Component的包装注解,譬如

@Configuration、@Service也可以。但本质还是注解@Component)

@EnableAutoConfiguration在Spring boot中是启动自动配置的(Auto-configuration tries to be as intelligent as possible and will back-away as you define more of your own configuration.)

yml:
map结构:

  1. myProps: #自定义的属性和值
  2. simpleProp: simplePropValue
  3. arrayProps: 1,2,3,4,5
  4. listProp1: #List中的元素是Map
  5. - name: abc
  6. value: abcValue
  7. - name: efg
  8. value: efgValue
  9. listProp2:
  10. - config2Value1
  11. - config2Vavlue2
  12. mapProps:
  13. key1: value1
  14. key2: value2
  1. @Component
  2. @ConfigurationProperties(prefix="myProps") //接收application.yml中的myProps下面的属性
  3. public class MyProps {
  4. private String simpleProp;
  5. private String[] arrayProps;
  6. private List<Map<String, String>> listProp1 = new ArrayList<>(); //接收prop1里面的属性值,List中的元素是Map
  7. private List<String> listProp2 = new ArrayList<>(); //接收prop2里面的属性值
  8. private Map<String, String> mapProps = new HashMap<>(); //接收prop1里面的属性值
  9.  
  10. public String getSimpleProp() {
  11. return simpleProp;
  12. }
  13.  
  14. //String类型的一定需要setter来接收属性值;maps, collections, 和 arrays 不需要
  15. public void setSimpleProp(String simpleProp) {
  16. this.simpleProp = simpleProp;
  17. }
  18.  
  19. public List<Map<String, String>> getListProp1() {
  20. return listProp1;
  21. }
  22. public List<String> getListProp2() {
  23. return listProp2;
  24. }
  25.  
  26. public String[] getArrayProps() {
  27. return arrayProps;
  28. }
  29.  
  30. public void setArrayProps(String[] arrayProps) {
  31. this.arrayProps = arrayProps;
  32. }
  33.  
  34. public Map<String, String> getMapProps() {
  35. return mapProps;
  36. }
  37.  
  38. public void setMapProps(Map<String, String> mapProps) {
  39. this.mapProps = mapProps;
  40. }
  41. }

http://blog.csdn.net/clementad/article/details/51970962

Take a look at the javadoc for ConfigurationProperties for more information on its various configuration options. For example, you can set a prefix to divide your configuration into a number of different namespaces:

@ConfigurationProperties(prefix="foo")
For the binding to work, you'd then have to use the same prefix on the properties declared in application.properties:

foo.users.alice=alpha
foo.users.bob=bravo

注入到Map类型的users字段中, alice和bob是map结构的key,等号后面的是value

一、@ConfigurationProperties注解的使用

看配置文件,我的是yaml格式的配置:

  1. // file application.yml
  2. my:
  3. servers:
  4. - dev.bar.com
  5. - foo.bar.com
  6. - jiaobuchong.com

下面我要将上面的配置属性注入到一个Java Bean类中,看码:

  1. import org.springframework.boot.context.properties.ConfigurationProperties;
  2. import org.springframework.stereotype.Component;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. @Component //不加这个注解的话, 使用@Autowired 就不能注入进去了
  6. @ConfigurationProperties(prefix = "my") // 配置文件中的前缀
  7. public class MyConfig {
  8. private List<String> servers = new ArrayList<String>();
  9. public List<String> getServers() { return this.servers;
  10. }
  11. }

下面写一个Controller来测试一下:

  1. @RequestMapping("/test")
  2. @RestController
  3. public class HelloController {
  4. @Autowired
  5. private MyConfig myConfig;
  6. @RequestMapping("/config")
  7. public Object getConfig() {
  8. return myConfig.getServers();
  9. }
  10. }

下面运行Application.java的main方法跑一下看看:

  1. @SpringBootApplication
  2. public class Application {
  3. public static void main(String[] args) {
  4. // 启动Spring Boot项目的唯一入口
  5. SpringApplication app = new SpringApplication(Application.class);
  6. app.setBannerMode(Banner.Mode.OFF);
  7. app.run(args);
  8. }

在浏览器的地址栏里输入: 
localhost:8080/test/config 得到: 
[“dev.bar.com”,”foo.bar.com”,”jiaobuchong.com”]

@EnableAutoConfiguration

  1. /**
  2. * Enable auto-configuration of the Spring Application Context, attempting to guess and configure beans that you are likely to need. Auto-configuration classes are usually
  1. * applied based on your classpath and what beans you have defined. For example, If you
  2. * have {@code tomcat-embedded.jar} on your classpath you are likely to want a
  3. * {@link TomcatEmbeddedServletContainerFactory} (unless you have defined your own
  4. * {@link EmbeddedServletContainerFactory} bean).
  5. * <p>
  6. * Auto-configuration tries to be as intelligent as possible and will back-away as you
  7. * define more of your own configuration. You can always manually {@link #exclude()} any
  8. * configuration that you never want to apply (use {@link #excludeName()} if you don't
  9. * have access to them). You can also exclude them via the
  10. * {@code spring.autoconfigure.exclude} property. Auto-configuration is always applied
  11. * after user-defined beans have been registered.
  12. * <p>
  13. * The package of the class that is annotated with {@code @EnableAutoConfiguration} has
  14. * specific significance and is often used as a 'default'. For example, it will be used
  15. * when scanning for {@code @Entity} classes. It is generally recommended that you place
  16. * {@code @EnableAutoConfiguration} in a root package so that all sub-packages and classes
  17. * can be searched.
  18. * <p>
  19. * Auto-configuration classes are regular Spring {@link Configuration} beans. They are
  20. * located using the {@link SpringFactoriesLoader} mechanism (keyed against this class).
  21. * Generally auto-configuration beans are {@link Conditional @Conditional} beans (most
  22. * often using {@link ConditionalOnClass @ConditionalOnClass} and
  23. * {@link ConditionalOnMissingBean @ConditionalOnMissingBean} annotations).
  24. *
  25. * @author Phillip Webb
  26. * @author Stephane Nicoll
  27. * @see ConditionalOnBean
  28. * @see ConditionalOnMissingBean
  29. * @see ConditionalOnClass
  30. * @see AutoConfigureAfter
  31. */
  32. @SuppressWarnings("deprecation")
  33. @Target(ElementType.TYPE)
  34. @Retention(RetentionPolicy.RUNTIME)
  35. @Documented
  36. @Inherited
  37. @AutoConfigurationPackage
  38. @Import(EnableAutoConfigurationImportSelector.class)
  39. public @interface EnableAutoConfiguration {

二、@ConfigurationProperties和@EnableConfigurationProperties注解结合使用

在spring boot中使用yaml进行配置的一般步骤是, 
1、yaml配置文件,这里假设:

  1. my:
  2. webserver:
  3. #HTTP 监听端口
  4. port: 80
  5. #嵌入Web服务器的线程池配置
  6. threadPool:
  7. maxThreads: 100
  8. minThreads: 8
  9. idleTimeout: 60000

2、

  1. //file MyWebServerConfigurationProperties.java
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3.  
  4. @ConfigurationProperties(prefix = "my.webserver") //没有使用@Component或@Confinguration,因此此对象不会注册到Spring容器中,需要@EnableConfigurationProperties引用
  5. public class MyWebServerConfigurationProperties {
  6. private int port;
  7. private ThreadPool threadPool;
  8.  
  9. public int getPort() {
  10. return port;
  11. }
  12.  
  13. public void setPort(int port) {
  14. this.port = port;
  15. }
  16.  
  17. public ThreadPool getThreadPool() {
  18. return threadPool;
  19. }
  20.  
  21. public void setThreadPool(ThreadPool threadPool) {
  22. this.threadPool = threadPool;
  23. }
  24.  
  25. public static class ThreadPool {
  26. private int maxThreads;
  27. private int minThreads;
  28. private int idleTimeout;
  29.  
  30. public int getIdleTimeout() {
  31. return idleTimeout;
  32. }
  33.  
  34. public void setIdleTimeout(int idleTimeout) {
  35. this.idleTimeout = idleTimeout;
  36. }
  37.  
  38. public int getMaxThreads() {
  39. return maxThreads;
  40. }
  41.  
  42. public void setMaxThreads(int maxThreads) {
  43. this.maxThreads = maxThreads;
  44. }
  45.  
  46. public int getMinThreads() {
  47. return minThreads;
  48. }
  49.  
  50. public void setMinThreads(int minThreads) {
  51. this.minThreads = minThreads;
  52. }
  53. }
  54. }

3、

  1. // file: MyWebServerConfiguration.java
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  4.  
  5. @Configuration
  6. @EnableConfigurationProperties(MyWebServerConfigurationProperties.class)
  7. public class MyWebServerConfiguration {
  8. @Autowired
  9. private MyWebServerConfigurationProperties properties;
  10. /**
  11. *下面就可以引用MyWebServerConfigurationProperties类 里的配置了
  12. */
  13. public void setMyconfig() {
  14. String port = properties.getPort();
  15. // ...........
  16. }
  17. }

The @EnableConfigurationProperties annotation is automatically applied to your project so that any beans annotated with @ConfigurationProperties will be configured from the Environment properties. This style of configuration works particularly well with the SpringApplication external YAML configuration.(引自spring boot官方手册)

三、@Bean配置第三方组件(Third-party configuration)

创建一个bean类:

  1. // file ThreadPoolBean.java
  2. public class ThreadPoolBean {
  3. private int maxThreads;
  4. private int minThreads;
  5. private int idleTimeout;
  6.  
  7. public int getMaxThreads() {
  8. return maxThreads;
  9. }
  10.  
  11. public void setMaxThreads(int maxThreads) {
  12. this.maxThreads = maxThreads;
  13. }
  14.  
  15. public int getMinThreads() {
  16. return minThreads;
  17. }
  18.  
  19. public void setMinThreads(int minThreads) {
  20. this.minThreads = minThreads;
  21. }
  22.  
  23. public int getIdleTimeout() {
  24. return idleTimeout;
  25. }
  26.  
  27. public void setIdleTimeout(int idleTimeout) {
  28. this.idleTimeout = idleTimeout;
  29. }
  30. }

引用前面第二部分写的配置类:MyWebServerConfiguration.java和MyWebServerConfigurationProperties.java以及yaml配置文件,现在修改MyWebServerConfiguration.java类:

  1. import com.jiaobuchong.springboot.domain.ThreadPoolBean;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. @Configuration //这是一个配置类,与@Service、@Component的效果类似。spring会扫描到这个类,@Bean才会生效,将ThreadPoolBean这个返回值类注册到spring上下文环境中
  7. @EnableConfigurationProperties(MyWebServerConfigurationProperties.class) //通过这个注解, 将MyWebServerConfigurationProperties这个类的配置到上下文环境中,本类中使用的@Autowired注解注入才能生效
  8. public class MyWebServerConfiguration {
  9. @SuppressWarnings("SpringJavaAutowiringInspection") //加这个注解让IDE 不报: Could not autowire
  10. @Autowired
  11. private MyWebServerConfigurationProperties properties;
  12. @Bean //@Bean注解在方法上,返回值是一个类的实例,并声明这个返回值(返回一个对象)是spring上下文环境中的一个bean
  13. public ThreadPoolBean getThreadBean() {
  14. MyWebServerConfigurationProperties.ThreadPool threadPool = properties.getThreadPool();
  15. ThreadPoolBean threadPoolBean = new ThreadPoolBean();
  16. threadPoolBean.setIdleTimeout(threadPool.getIdleTimeout());
  17. threadPoolBean.setMaxThreads(threadPool.getMaxThreads());
  18. threadPoolBean.setMinThreads(threadPool.getMinThreads());
  19. return threadPoolBean;
  20. }
  21. }

被@Configuration注解标识的类,通常作为一个配置类,这就类似于一个xml文件,表示在该类中将配置Bean元数据,其作用类似于Spring里面application-context.xml的配置文件,而@Bean标签,则类似于该xml文件中,声明的一个bean实例。

写一个controller测试一下:

  1. import com.jiaobuchong.springboot.domain.ThreadPoolBean;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5.  
  6. @RequestMapping("/first")
  7. @RestController
  8. public class HelloController {
  9. @Autowired
  10. private ThreadPoolBean threadPoolBean;
  11. @RequestMapping("/testbean")
  12. public Object getThreadBean() {
  13. return threadPoolBean;
  14. }
  15. }

运行Application.java的main方法, 
在浏览器里输入:http://localhost:8080/first/testbean 
得到的返回值是: 
{“maxThreads”:100,”minThreads”:8,”idleTimeout”:60000}

http://blog.csdn.net/jiaobuchong/article/details/50442709

el最常用的几种使用场景:

  • 从配置文件中读取属性
  • 缺失值情况下,配置默认值
  • el内部字符串使用String的方法
  • 三目运算符
  • 正则表达式
  • 注入系统属性(system properties)
  • 调用系统原有函数
  • 直接注入文件进行操作
  • 读取另一个bean的函数的返回值

1、从配置文件中读取属性

application.properties

  1. name=\u8D75\u8BA1\u521A
  1. 1 @Value("${name}")//这里必须使用$,使用#是不行的
  2. 2 private String name;
  3. 3 @RequestMapping(value = "/name", method = RequestMethod.GET)
  4. 4 public String getName() {
  5. 5 return name;
  6. 6 }

注意:记住下边三句话

  • ${}不支持表达式(三目表达式不算表达式);#{}支持
  • ${}读取属性文件的值
  • ${}读取最后一个满足条件的值;#{}读取所有满足条件的值

2、缺失值情况下,配置默认值

  1. 1 @Value("${name2:'刚子'}")//这里必须使用$,使用#是不行的
  2. 2 private String name2;
  3. 3 @RequestMapping(value = "/name2", method = RequestMethod.GET)
  4. 4 public String getName2() {
  5. 5 return name2;
  6. 6 }

3、el内部字符串使用String的方法

  1. 1 name.list=\u8D75\u8BA1\u521A,\u738B\u5A1C
  1. 1 @Value("#{'${name.list}'.split(',')}")
  2. 2 private List<String> nameList;
  3. 3 @RequestMapping(value = "/nameList", method = RequestMethod.GET)
  4. 4 public List<String> getNameList() {
  5. 5 return nameList;
  6. 6 }

4、三目运算符

  1. 1 name.three=\u6768\u8FC7
  1. 1 @RequestMapping(value = "/nameThree", method = RequestMethod.GET)
  2. 2 public String getNameThree(@Value("${name.three!='杨过'?'黄蓉':'小龙女'}") String nameThree) {
  3. 3 return nameThree;
  4. 4 }

注意:@value可以直接作为入参

5、正则表达式

  1. 1 @Value("#{'100' matches '\\d+'}")//这里必须使用#,使用$是不行的
  2. 2 private boolean isDigital;
  3. 3 @RequestMapping(value = "/nameRegEx", method = RequestMethod.GET)
  4. 4 public boolean getNameRegEx() {
  5. 5 return isDigital;
  6. 6 }

6、注入系统属性

  1. 1 @Value("#{systemProperties['os.name']}")//这里必须使用#,使用$是不行的
  2. 2 private String osName;
  3. 3 @RequestMapping(value = "/osName", method = RequestMethod.GET)
  4. 4 public String getOsName() {
  5. 5 return osName;
  6. 6 }

7、调用系统原有函数

  1. 1 @Value("#{T(java.lang.Math).random() * 10}")//注意:这里只生成一次,之后你无论调用多少次getRandomValue(),都返回同一个值
  2. 2 private String randomValue;
  3. 3 @RequestMapping(value = "/randomValue", method = RequestMethod.GET)
  4. 4 public String getRandomValue() {
  5. 5 return randomValue;
  6. 6 }

8、直接注入文件进行操作

testfile/testEl.txt

  1. 1 乔峰--降龙十八掌
  2. 2 杨过--黯然销魂掌
  1. 1 @Value("classpath:testfile/testEl.txt")
  2. 2 private Resource txtResource;
  3. 3 @RequestMapping(value = "/resource", method = RequestMethod.GET)
  4. 4 public String getResource() {
  5. 5 try {
  6. 6 return IOUtils.toString(txtResource.getInputStream(), "UTF-8");
  7. 7 } catch (IOException e) {
  8. 8 ExceptionUtils.getStackTrace(e);
  9. 9 }
  10. 10 return StringUtils.EMPTY;
  11. 11 }

注意:这个很重要,操作文件使用Apache.commons.io.IOUtils来操作。

9、读取另一个bean的函数的返回值

  1. 1 @Component("eLComponent")
  2. 2 public class ELComponent {
  3. 3 public String getNameBean(){
  4. 4 return "乔峰";
  5. 5 }
  6. 6 }
  1. 1 @Value("#{eLComponent.getNameBean()}")//这里必须使用#,使用$是不行的
  2. 2 private String nameBean;
  3. 3 @RequestMapping(value = "/nameBean", method = RequestMethod.GET)
  4. 4 public String getNameBean() {
  5. 5 return nameBean;
  6. 6 }

注意:被调用bean最好指定一下beanName。

http://www.cnblogs.com/java-zhao/archive/2016/07/01/5633881.html

application.properties中的配置:

  1. db.driverClassName=com.mysql.jdbc.Driver
  2. db.url=jdbc:mysql://localhost:3306/mydb?characterEncoding=utf-8&useSSL=false
  3. db.username=xxx
  4. db.password=xxx
  1. import java.beans.PropertyVetoException;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.core.env.Environment;
  7. import com.mchange.v2.c3p0.ComboPooledDataSource;
  8.  
  9. @Configuration
  10. public class DBconfig {
  11. @Autowired
  12. private Environment env;
  13.  
  14. @Bean(name="dataSource")
  15. public ComboPooledDataSource dataSource() throws PropertyVetoException {
  16. ComboPooledDataSource dataSource = new ComboPooledDataSource();
  17. dataSource.setDriverClass(env.getProperty("db.driverClassName"));
  18. dataSource.setJdbcUrl(env.getProperty("db.url"));
  19. dataSource.setUser(env.getProperty("db.username"));
  20. dataSource.setPassword(env.getProperty("db.password"));
  21. dataSource.setMaxPoolSize(20);
  22. dataSource.setMinPoolSize(5);
  23. dataSource.setInitialPoolSize(10);
  24. dataSource.setMaxIdleTime(300);
  25. dataSource.setAcquireIncrement(5);
  26. dataSource.setIdleConnectionTestPeriod(60);
  27. return dataSource;
  28. }
  29. }

Spring boot将配置属性注入到bean类中的更多相关文章

  1. Spring boot将配置属性注入到bean 专题

    https://blog.csdn.net/wangmx1993328/article/details/81002901 Error starting ApplicationContext. To d ...

  2. spring boot 全局配置属性一览

    # =================================================================== # COMMON SPRING BOOT PROPERTIE ...

  3. Spring boot 将配置文件属性注入到一个bean中

    现在要做的就是将如下配置文件中的内容注入到一个bean 名为Properties中. Redis.properties配置文件中的内容如下: Properties java bean中代码如下,注意注 ...

  4. Sping Boot入门到实战之入门篇(四):Spring Boot自动化配置

    该篇为Sping Boot入门到实战系列入门篇的第四篇.介绍Spring Boot自动化配置的基本原理与实现.   Spring Boot之所以受开发者欢迎, 其中最重要的一个因素就是其自动化配置特性 ...

  5. 玩转Spring Boot 自定义配置、导入XML配置与外部化配置

    玩转Spring Boot 自定义配置.导入XML配置与外部化配置       在这里我会全面介绍在Spring Boot里面如何自定义配置,更改Spring Boot默认的配置,以及介绍各配置的优先 ...

  6. Spring Boot -- 外部配置的属性使用

    Spring Boot允许使用propertities文件.yaml文件或者命令行参数作为外部配置. 命令行参数配置 Spring Boot可以基于jar包运行,打成jar包的程序可以直接通过下面的命 ...

  7. Spring Boot通过ImportBeanDefinitionRegistrar动态注入Bean

    在阅读Spring Boot源码时,看到Spring Boot中大量使用ImportBeanDefinitionRegistrar来实现Bean的动态注入.它是Spring中一个强大的扩展接口.本篇文 ...

  8. 玩转spring boot——properties配置

    前言 在以往的java开发中,程序员最怕大量的配置,是因为配置一多就不好统一管理,经常出现找不到配置的情况.而项目中,从开发测试环境到生产环境,往往需要切换不同的配置,如测试数据库连接换成生产数据库连 ...

  9. Spring Boot基本配置

    本文参考javaEE开发的颠覆者SpringBoot实战第一版 基本配置 入口类和@SpringBootApplication Spring Boot通常有一个名为*Application的入口类,且 ...

随机推荐

  1. 物联网MQTT协议分析和开源Mosquitto部署验证

    在<物联网核心协议—消息推送技术演进>一文中已向读者介绍了多种消息推送技术的情况,包括HTTP单向通信.Ajax轮询.Websocket.MQTT.CoAP等,其中MQTT协议为IBM制定 ...

  2. HDU 5040 Instrusive(BFS+优先队列)

    题意比较啰嗦. 就是搜索加上一些特殊的条件,比如可以在原地不动,也就是在原地呆一秒,如果有监控也可以花3秒的时间走过去. 这种类型的题目还是比较常见的.以下代码b[i][j][x]表示格子i行j列在x ...

  3. centos7 部署ssserver

    centos7 部署shadowsocks服务端 为什么要选centos7? 以后centos7 肯定是主流,在不重要的环境还是尽量使用新系统吧 centos7 的坑 默认可能会有firewall 或 ...

  4. 基于Bootstrap实现下图所示效果的页面,一个白底的带有两个菜单项、一个下拉菜单和一个登录表单的基本导航条

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8& ...

  5. js 实现关键词球状旋转效果

    效果图 html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...

  6. jquery获取元素到页面顶部距离

    jquery获取元素到页面顶部距离的语句为: $(selector).offset().top

  7. Windows 2008 server R2安装.NET Framework4时提示“灾难性故障”解决

    在安装行环境.NET Framework 4时无法安装,提示“灾难性故障”.服务器的操作系统是windows  server 2008 R2. 查看系统日志时显示“无法安装 Windows 更新 &q ...

  8. Dedecms当前位置{dede:field name='position'/}修改

    这个实在list_article.htm模板出现的,而这个模板通过loadtemplage等等一系列操作是调用的include 下的arc.archives.class.php $this->F ...

  9. android launcher3 home页简易分析

    最近在修改一个问题:就是修改home页下,用户手动拖出来的APP图片下面的字体显示不全,思路比较明确,需要尽量加大整个APP控件的高度,或者缩小图片和文字之间的间隔. 跟代码发现APP整个控件的lay ...

  10. Data Types

    原地址: Home / Database / Oracle Database Online Documentation 11g Release 2 (11.2) / Database Administ ...