十四,SpringBoot读取配置文件application.properties
方法1:Environment
@Autowired//import org.springframework.core.env.Environment; private Environment environment; @org.junit.Test public void readConfigFile(){ System.out.println(environment.getProperty("spring.datasource.driverClassName")); }
控制台打印:
"C:\Program Files\Java\jdk-11.0.7\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:E:\IntelliJ IDEA 2019.1\lib\idea_rt.jar=4872:E:\IntelliJ IDEA 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "E:\IntelliJ IDEA 2019.1\lib\idea_rt.jar;E:\IntelliJ IDEA 2019.1\plugins\junit\lib\junit-rt.jar;E:\IntelliJ IDEA 2019.1\plugins\junit\lib\junit5-rt.jar;D:\学习代码\java\cleanSpringBoot\target\test-classes;D:\学习代码\java\cleanSpringBoot\target\classes;F:\apache-maven-3.6.0\repository\org\springframework\boot\spring-boot-starter-web\2.3.1.RELEASE\spring-boot-starter-web-2.3.1.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\boot\spring-boot-starter\2.3.1.RELEASE\spring-boot-starter-2.3.1.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\boot\spring-boot-starter-logging\2.3.1.RELEASE\spring-boot-starter-logging-2.3.1.RELEASE.jar;F:\apache-maven-3.6.0\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;F:\apache-maven-3.6.0\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;F:\apache-maven-3.6.0\repository\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;F:\apache-maven-3.6.0\repository\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;F:\apache-maven-3.6.0\repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;F:\apache-maven-3.6.0\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;F:\apache-maven-3.6.0\repository\org\yaml\snakeyaml\1.26\snakeyaml-1.26.jar;F:\apache-maven-3.6.0\repository\org\springframework\boot\spring-boot-starter-json\2.3.1.RELEASE\spring-boot-starter-json-2.3.1.RELEASE.jar;F:\apache-maven-3.6.0\repository\com\fasterxml\jackson\core\jackson-databind\2.11.0\jackson-databind-2.11.0.jar;F:\apache-maven-3.6.0\repository\com\fasterxml\jackson\core\jackson-annotations\2.11.0\jackson-annotations-2.11.0.jar;F:\apache-maven-3.6.0\repository\com\fasterxml\jackson\core\jackson-core\2.11.0\jackson-core-2.11.0.jar;F:\apache-maven-3.6.0\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.11.0\jackson-datatype-jdk8-2.11.0.jar;F:\apache-maven-3.6.0\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.11.0\jackson-datatype-jsr310-2.11.0.jar;F:\apache-maven-3.6.0\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.11.0\jackson-module-parameter-names-2.11.0.jar;F:\apache-maven-3.6.0\repository\org\springframework\boot\spring-boot-starter-tomcat\2.3.1.RELEASE\spring-boot-starter-tomcat-2.3.1.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.36\tomcat-embed-core-9.0.36.jar;F:\apache-maven-3.6.0\repository\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;F:\apache-maven-3.6.0\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.36\tomcat-embed-websocket-9.0.36.jar;F:\apache-maven-3.6.0\repository\org\springframework\spring-web\5.2.7.RELEASE\spring-web-5.2.7.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\spring-beans\5.2.7.RELEASE\spring-beans-5.2.7.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\spring-webmvc\5.2.7.RELEASE\spring-webmvc-5.2.7.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\spring-aop\5.2.7.RELEASE\spring-aop-5.2.7.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\spring-context\5.2.7.RELEASE\spring-context-5.2.7.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\spring-expression\5.2.7.RELEASE\spring-expression-5.2.7.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\boot\spring-boot-devtools\2.3.1.RELEASE\spring-boot-devtools-2.3.1.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\boot\spring-boot\2.3.1.RELEASE\spring-boot-2.3.1.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\boot\spring-boot-autoconfigure\2.3.1.RELEASE\spring-boot-autoconfigure-2.3.1.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\boot\spring-boot-starter-data-jpa\2.3.1.RELEASE\spring-boot-starter-data-jpa-2.3.1.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\boot\spring-boot-starter-aop\2.3.1.RELEASE\spring-boot-starter-aop-2.3.1.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\aspectj\aspectjweaver\1.9.5\aspectjweaver-1.9.5.jar;F:\apache-maven-3.6.0\repository\org\springframework\boot\spring-boot-starter-jdbc\2.3.1.RELEASE\spring-boot-starter-jdbc-2.3.1.RELEASE.jar;F:\apache-maven-3.6.0\repository\com\zaxxer\HikariCP\3.4.5\HikariCP-3.4.5.jar;F:\apache-maven-3.6.0\repository\org\springframework\spring-jdbc\5.2.7.RELEASE\spring-jdbc-5.2.7.RELEASE.jar;F:\apache-maven-3.6.0\repository\jakarta\transaction\jakarta.transaction-api\1.3.3\jakarta.transaction-api-1.3.3.jar;F:\apache-maven-3.6.0\repository\jakarta\persistence\jakarta.persistence-api\2.2.3\jakarta.persistence-api-2.2.3.jar;F:\apache-maven-3.6.0\repository\org\hibernate\hibernate-core\5.4.17.Final\hibernate-core-5.4.17.Final.jar;F:\apache-maven-3.6.0\repository\org\jboss\logging\jboss-logging\3.4.1.Final\jboss-logging-3.4.1.Final.jar;F:\apache-maven-3.6.0\repository\org\javassist\javassist\3.24.0-GA\javassist-3.24.0-GA.jar;F:\apache-maven-3.6.0\repository\net\bytebuddy\byte-buddy\1.10.11\byte-buddy-1.10.11.jar;F:\apache-maven-3.6.0\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar;F:\apache-maven-3.6.0\repository\org\jboss\jandex\2.1.3.Final\jandex-2.1.3.Final.jar;F:\apache-maven-3.6.0\repository\com\fasterxml\classmate\1.5.1\classmate-1.5.1.jar;F:\apache-maven-3.6.0\repository\org\dom4j\dom4j\2.1.3\dom4j-2.1.3.jar;F:\apache-maven-3.6.0\repository\org\hibernate\common\hibernate-commons-annotations\5.1.0.Final\hibernate-commons-annotations-5.1.0.Final.jar;F:\apache-maven-3.6.0\repository\org\glassfish\jaxb\jaxb-runtime\2.3.3\jaxb-runtime-2.3.3.jar;F:\apache-maven-3.6.0\repository\org\glassfish\jaxb\txw2\2.3.3\txw2-2.3.3.jar;F:\apache-maven-3.6.0\repository\com\sun\istack\istack-commons-runtime\3.0.11\istack-commons-runtime-3.0.11.jar;F:\apache-maven-3.6.0\repository\com\sun\activation\jakarta.activation\1.2.2\jakarta.activation-1.2.2.jar;F:\apache-maven-3.6.0\repository\org\springframework\data\spring-data-jpa\2.3.1.RELEASE\spring-data-jpa-2.3.1.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\data\spring-data-commons\2.3.1.RELEASE\spring-data-commons-2.3.1.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\spring-orm\5.2.7.RELEASE\spring-orm-5.2.7.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\spring-tx\5.2.7.RELEASE\spring-tx-5.2.7.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;F:\apache-maven-3.6.0\repository\org\springframework\spring-aspects\5.2.7.RELEASE\spring-aspects-5.2.7.RELEASE.jar;F:\apache-maven-3.6.0\repository\mysql\mysql-connector-java\8.0.20\mysql-connector-java-8.0.20.jar;F:\apache-maven-3.6.0\repository\org\mybatis\spring\boot\mybatis-spring-boot-starter\1.1.1\mybatis-spring-boot-starter-1.1.1.jar;F:\apache-maven-3.6.0\repository\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\1.1.1\mybatis-spring-boot-autoconfigure-1.1.1.jar;F:\apache-maven-3.6.0\repository\org\mybatis\mybatis\3.4.0\mybatis-3.4.0.jar;F:\apache-maven-3.6.0\repository\org\mybatis\mybatis-spring\1.3.0\mybatis-spring-1.3.0.jar;F:\apache-maven-3.6.0\repository\org\springframework\boot\spring-boot-starter-redis\1.3.8.RELEASE\spring-boot-starter-redis-1.3.8.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\data\spring-data-redis\2.3.1.RELEASE\spring-data-redis-2.3.1.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\data\spring-data-keyvalue\2.3.1.RELEASE\spring-data-keyvalue-2.3.1.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\spring-oxm\5.2.7.RELEASE\spring-oxm-5.2.7.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\spring-context-support\5.2.7.RELEASE\spring-context-support-5.2.7.RELEASE.jar;F:\apache-maven-3.6.0\repository\redis\clients\jedis\3.3.0\jedis-3.3.0.jar;F:\apache-maven-3.6.0\repository\org\apache\commons\commons-pool2\2.8.0\commons-pool2-2.8.0.jar;F:\apache-maven-3.6.0\repository\org\springframework\boot\spring-boot-starter-test\2.3.1.RELEASE\spring-boot-starter-test-2.3.1.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\boot\spring-boot-test\2.3.1.RELEASE\spring-boot-test-2.3.1.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\boot\spring-boot-test-autoconfigure\2.3.1.RELEASE\spring-boot-test-autoconfigure-2.3.1.RELEASE.jar;F:\apache-maven-3.6.0\repository\com\jayway\jsonpath\json-path\2.4.0\json-path-2.4.0.jar;F:\apache-maven-3.6.0\repository\net\minidev\json-smart\2.3\json-smart-2.3.jar;F:\apache-maven-3.6.0\repository\net\minidev\accessors-smart\1.2\accessors-smart-1.2.jar;F:\apache-maven-3.6.0\repository\org\ow2\asm\asm\5.0.4\asm-5.0.4.jar;F:\apache-maven-3.6.0\repository\jakarta\xml\bind\jakarta.xml.bind-api\2.3.3\jakarta.xml.bind-api-2.3.3.jar;F:\apache-maven-3.6.0\repository\jakarta\activation\jakarta.activation-api\1.2.2\jakarta.activation-api-1.2.2.jar;F:\apache-maven-3.6.0\repository\org\assertj\assertj-core\3.16.1\assertj-core-3.16.1.jar;F:\apache-maven-3.6.0\repository\org\hamcrest\hamcrest\2.2\hamcrest-2.2.jar;F:\apache-maven-3.6.0\repository\org\junit\jupiter\junit-jupiter\5.6.2\junit-jupiter-5.6.2.jar;F:\apache-maven-3.6.0\repository\org\junit\jupiter\junit-jupiter-api\5.6.2\junit-jupiter-api-5.6.2.jar;F:\apache-maven-3.6.0\repository\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;F:\apache-maven-3.6.0\repository\org\junit\platform\junit-platform-commons\1.6.2\junit-platform-commons-1.6.2.jar;F:\apache-maven-3.6.0\repository\org\junit\jupiter\junit-jupiter-params\5.6.2\junit-jupiter-params-5.6.2.jar;F:\apache-maven-3.6.0\repository\org\junit\jupiter\junit-jupiter-engine\5.6.2\junit-jupiter-engine-5.6.2.jar;F:\apache-maven-3.6.0\repository\org\junit\vintage\junit-vintage-engine\5.6.2\junit-vintage-engine-5.6.2.jar;F:\apache-maven-3.6.0\repository\org\apiguardian\apiguardian-api\1.1.0\apiguardian-api-1.1.0.jar;F:\apache-maven-3.6.0\repository\org\junit\platform\junit-platform-engine\1.6.2\junit-platform-engine-1.6.2.jar;F:\apache-maven-3.6.0\repository\junit\junit\4.13\junit-4.13.jar;F:\apache-maven-3.6.0\repository\org\mockito\mockito-core\3.3.3\mockito-core-3.3.3.jar;F:\apache-maven-3.6.0\repository\net\bytebuddy\byte-buddy-agent\1.10.11\byte-buddy-agent-1.10.11.jar;F:\apache-maven-3.6.0\repository\org\objenesis\objenesis\2.6\objenesis-2.6.jar;F:\apache-maven-3.6.0\repository\org\mockito\mockito-junit-jupiter\3.3.3\mockito-junit-jupiter-3.3.3.jar;F:\apache-maven-3.6.0\repository\org\skyscreamer\jsonassert\1.5.0\jsonassert-1.5.0.jar;F:\apache-maven-3.6.0\repository\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;F:\apache-maven-3.6.0\repository\org\springframework\spring-core\5.2.7.RELEASE\spring-core-5.2.7.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\spring-jcl\5.2.7.RELEASE\spring-jcl-5.2.7.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\springframework\spring-test\5.2.7.RELEASE\spring-test-5.2.7.RELEASE.jar;F:\apache-maven-3.6.0\repository\org\xmlunit\xmlunit-core\2.7.0\xmlunit-core-2.7.0.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.example.test.Test,readConfigFile 23:50:07.958 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.example.test.Test] 23:50:07.972 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate] 23:50:07.990 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)] 23:50:08.116 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.example.test.Test] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper] 23:50:08.166 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.example.test.Test], using SpringBootContextLoader 23:50:08.180 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.example.test.Test]: class path resource [com/example/test/Test-context.xml] does not exist 23:50:08.183 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.example.test.Test]: class path resource [com/example/test/TestContext.groovy] does not exist 23:50:08.183 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.example.test.Test]: no resource found for suffixes {-context.xml, Context.groovy}. 23:50:08.286 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.example.test.Test] 23:50:08.587 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.example.test.Test]: using defaults. 23:50:08.588 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener] 23:50:08.644 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@1ebea008, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@72d6b3ba, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@1787f2a0, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@7de62196, org.springframework.test.context.support.DirtiesContextTestExecutionListener@163370c2, org.springframework.test.context.transaction.TransactionalTestExecutionListener@51bf5add, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@7905a0b8, org.springframework.test.context.event.EventPublishingTestExecutionListener@35a3d49f, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@389b0789, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@13d9cbf5, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@478db956, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@6ca18a14, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@c667f46, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@51bd8b5c] 23:50:08.650 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.example.test.Test] 23:50:08.655 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.example.test.Test] 23:50:08.658 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.example.test.Test] 23:50:08.659 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.example.test.Test] 23:50:08.659 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.example.test.Test] 23:50:08.659 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.example.test.Test]23:50:08.694 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.example.test.Test] 23:50:08.694 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.example.test.Test]23:50:08.697 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.example.test.Test] 23:50:08.697 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.example.test.Test] 23:50:08.701 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.example.test.Test] 23:50:08.702 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.example.test.Test] 23:50:08.719 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@6692b6c6 testClass = Test, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@1cd629b3 testClass = Test, locations = '{}', classes = '{class com.CleanSpringBootApplication, class com.CleanSpringBootApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@4988d8b8, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@6913c1fb, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@7ee8290b, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@15ca7889, org.springframework.boot.test.context.SpringBootTestArgs@1], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true]], class annotated with @DirtiesContext [false] with mode [null]. 23:50:08.723 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.example.test.Test] 23:50:08.725 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.example.test.Test] 23:50:08.818 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true} . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.1.RELEASE) 2020-07-18 23:50:10.901 INFO 16360 --- [ main] com.example.test.Test : Starting Test on DESKTOP-2PVNKVI with PID 16360 (started by Administrator in D:\学习代码\java\cleanSpringBoot) 2020-07-18 23:50:10.908 INFO 16360 --- [ main] com.example.test.Test : No active profile set, falling back to default profiles: default 2020-07-18 23:50:18.360 INFO 16360 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode! 2020-07-18 23:50:18.363 INFO 16360 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode. 2020-07-18 23:50:19.856 INFO 16360 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 1471ms. Found 1 JPA repository interfaces. 2020-07-18 23:50:19.889 INFO 16360 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode! 2020-07-18 23:50:19.890 INFO 16360 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode. 2020-07-18 23:50:20.968 INFO 16360 --- [ main] .RepositoryConfigurationExtensionSupport : Spring Data Redis - Could not safely identify store assignment for repository candidate interface com.example.dao.IUserDao. If you want this repository to be a Redis repository, consider annotating your entities with one of these annotations: org.springframework.data.redis.core.RedisHash (preferred), or consider extending one of the following types with your repository: org.springframework.data.keyvalue.repository.KeyValueRepository. 2020-07-18 23:50:20.969 INFO 16360 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 1060ms. Found 0 Redis repository interfaces. Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. 2020-07-18 23:50:23.201 INFO 16360 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2020-07-18 23:50:24.088 INFO 16360 --- [ task-1] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default] 2020-07-18 23:50:24.479 INFO 16360 --- [ task-1] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.17.Final 2020-07-18 23:50:25.672 INFO 16360 --- [ task-1] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final} 2020-07-18 23:50:26.227 WARN 16360 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning 2020-07-18 23:50:27.203 INFO 16360 --- [ task-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2020-07-18 23:50:27.218 WARN 16360 --- [ task-1] com.zaxxer.hikari.util.DriverDataSource : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation. 2020-07-18 23:50:28.376 INFO 16360 --- [ task-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 2020-07-18 23:50:28.424 INFO 16360 --- [ task-1] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect 2020-07-18 23:50:28.473 INFO 16360 --- [ main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories… 2020-07-18 23:50:30.315 INFO 16360 --- [ task-1] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform] 2020-07-18 23:50:30.334 INFO 16360 --- [ task-1] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' 2020-07-18 23:50:30.720 INFO 16360 --- [ main] DeferredRepositoryInitializationListener : Spring Data repositories initialized! 2020-07-18 23:50:30.736 INFO 16360 --- [ main] com.example.test.Test : Started Test in 21.891 seconds (JVM running for 24.626) com.mysql.jdbc.Driver 2020-07-18 23:50:31.000 INFO 16360 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default' 2020-07-18 23:50:31.003 INFO 16360 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor' 2020-07-18 23:50:31.005 INFO 16360 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... 2020-07-18 23:50:31.041 INFO 16360 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed. Process finished with exit code 0
方法2:绑定对象bean调用
1、在application.properties或者application.yml里面添加我们的配置项:
2、创建一个配置类(该类和我们上面配置项的属性一一对应):
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "upload") public class PropertiesConfig { private int port; public int getPort() { return port; } public void setPort(int port) { this.port = port; } }
3、在需要调用的地方自动装配上我们上面的类并按照下面的方式获得配置项的值:
@Controller @RequestMapping("/update/send") public class UpdateSendController extends BaseController { @Autowired private PropertiesConfig propertiesconfig; int port = propertiesconfig.getPort(); }