SpringBoot使用一個全局的配置文件,配置文件名是固定的,配置文件主要分爲如下兩種html
•application.propertiesjava
•application.ymlreact
配置文件的做用:修改SpringBoot自動配置的默認值;SpringBoot在底層都給咱們自動配置好;git
YAML(YAML Ain't Markup Language)github
YAML A Markup Language:是一個標記語言web
YAML isn't Markup Language:不是一個標記語言;redis
標記語言:spring
之前的配置文件;大多都使用的是 xxxx.xml文件;apache
YAML:以數據爲中心,比json、xml等更適合作配置文件;json
YAML:配置例子
server: port: 8081
XML:
<server> <port>8081</port> </server>
一、基本語法
k:(空格)v:表示一對鍵值對(空格必須有);
以空格的縮進來控制層級關係;只要是左對齊的一列數據,都是同一個層級的
server: port: 8081 path: /hello
屬性和值也是大小寫敏感;
k: v:字面直接來寫;
字符串默認不用加上單引號或者雙引號;
"":雙引號;不會轉義字符串裏面的特殊字符;特殊字符會做爲自己想表示的意思
name: "zhangsan \n lisi":輸出;zhangsan 換行 lisi
'':單引號;會轉義特殊字符,特殊字符最終只是一個普通的字符串數據
name: ‘zhangsan \n lisi’:輸出;zhangsan \n lisi
k: v:在下一行來寫對象的屬性和值的關係;注意縮進
對象仍是k: v的方式
friends: lastName: zhangsan age: 20
行內寫法:
friends: {lastName: zhangsan,age: 18}
用- 值表示數組中的一個元素
pets: - cat - dog - pig people: - name - age - address
行內寫法
pets: [cat,dog,pig]
配置文件
person: lastName: hello age: 18 boss: false birth: 2017/12/12 maps: {k1: v1,k2: 12} lists: - lisi - zhaoliu dog: name: 小狗 age: 12
javaBean:
/** * 將配置文件中配置的每個屬性的值,映射到這個組件中 * @ConfigurationProperties:告訴SpringBoot將本類中的全部屬性和配置文件中相關的配置進行綁定; * prefix = "person":配置文件中哪一個下面的全部屬性進行一一映射 * * 只有這個組件是容器中的組件,才能容器提供的@ConfigurationProperties功能; * */ @Component @ConfigurationProperties(prefix = "person") public class Person { private String lastName; private Integer age; private Boolean boss; private Date birth; private Map<String,Object> maps; private List<Object> lists; private Dog dog;
咱們能夠導入配置文件處理器,之後編寫配置就有提示了
<!--導入配置文件處理器,配置文件進行綁定就會有提示--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
個人單元測試裏面顯示bean對象注入失敗,咱們都知道spring中使用註解會在spring配置文件中開啓掃描註解的方法,springmvc也是同樣,那麼spring-boot也會提供一個掃描註解的方法叫作@ComponentScan(basePackages = {"com.bdqn.Controller"}),表示會掃描到basePackages中全部約定包下面的全部註解
後來經研究發現,SpringBoot項目的Bean裝配默認規則是根據Application類所在的包位置從上往下掃描! 「Application類」是指SpringBoot項目入口類。這個類的位置很關鍵:
若是Application類所在的包爲:io.github.gefangshuai.app
,則只會掃描io.github.gefangshuai.app
包及其全部子包,若是service或dao所在包不在io.github.gefangshuai.app
及其子包下,則不會被掃描!
本次測試的是yml配置文件映射動態綁定到實體類中,可是在單元測試類中出現bean對象掃描失敗
這裏給你們說明兩點要注意的地方
①測試文件的項目名稱要和啓動類的項目名稱保持一致
②掃描註解只有放在啓動類上面纔會起到掃描bean對象的做用
實現步驟以下
/*** * 將配置文件中配置的每個值映射到這個組件中 * * @ConfigurationProperties:告訴SpringBoot將本類中的全部屬性和配置文件中相關的配置進行綁定; * * prefix = "person":配置文件中哪一個下面的全部屬性進行一一映射 * * * * 只有這個組件是容器中的組件,才能容器提供的@ConfigurationProperties功能; * * * */ @Component //把實體類注入容器 @ConfigurationProperties(prefix = "person") public class Person { // alt+insert private String lastName; private Integer age; private Boolean boss; private Date birth; private Map<String,Object> maps; private List<Object> lists; private Dog dog;
###aplication.yml配置文件 person: lastName: hello age: 18 boss: false birth: 2017/12/12 maps: {k1: v1,k2: 12} lists: - lisi - zhaoliu dog: name: 小狗 age: 12
springboot啓動類
@SpringBootApplication @ComponentScan(basePackages = {"com.bdqn"}) /** * 當測試啓動類和Controller不位於同一個包下面時候須要 * 配置@ComponentScan(basePackages = {"com.bdqn.Controller"})去掃描controller的路徑 */ public class SpringBootQuickApplication { public static void main(String[] args) { SpringApplication.run(SpringBootQuickApplication.class, args); } }
在springboot提供的單元測試類裏面進行測試
啓動單元測試類,拿到了application.yml文件中配置的值
配置文件yml仍是properties他們都能獲取到值;
若是說,咱們只是在某個業務邏輯中須要獲取一下配置文件中的某項值,使用@Value;
測試代碼以下
application.properties文件,實體類的代碼仍是不變
##修改spring-boot給咱們底層的默認配置 #默認是的的是utf-8 #配置person的值 person.last-name=tom person.age=18 person.boss=true person.birth=2017/10/1 person.maps.k1=v1 person.maps.k2=v2 person.lists=a,b,c person.dog.name=dog person.dog.age=15
@RestController /** * 至關於 Contoller和RequestBody的合體,都是把數據響應給瀏覽器 */ @RequestMapping("/test") public class HelloController { @Value("${person.last-name}") private String name; @RequestMapping("/hello") public String hello(){ return "hello you name is"+name; } }
@Component @ConfigurationProperties(prefix = "person") @Validated //專門驗證數據的格式是否正確 public class Person { /** * <bean class="Person"> * <property name="lastName" value="字面量/${key}從環境變量、配置文件中獲取值/#{SpEL}"></property> * <bean/> */ //lastName必須是郵箱格式 @Email //@Value("${person.last-name}") private String lastName; //@Value("#{11*2}") private Integer age; //@Value("true") private Boolean boss; private Date birth; private Map<String,Object> maps; private List<Object> lists; private Dog dog;
/** * 將配置文件中配置的每個屬性的值,映射到這個組件中 * @ConfigurationProperties:告訴SpringBoot將本類中的全部屬性和配置文件中相關的配置進行綁定; * prefix = "person":配置文件中哪一個下面的全部屬性進行一一映射 * * 只有這個組件是容器中的組件,才能容器提供的@ConfigurationProperties功能; * @ConfigurationProperties(prefix = "person")默認從全局配置文件中獲取值; * */ @PropertySource(value = {"classpath:person.properties"}) @Component @ConfigurationProperties(prefix = "person") //@Validated public class Person { /** * <bean class="Person"> * <property name="lastName" value="字面量/${key}從環境變量、配置文件中獲取值/#{SpEL}"></property> * <bean/> */ //lastName必須是郵箱格式 // @Email //@Value("${person.last-name}") private String lastName; //@Value("#{11*2}") private Integer age; //@Value("true") private Boolean boss;
@ImportResource:導入Spring的配置文件,讓配置文件裏面的內容生效;
Spring Boot裏面沒有Spring的配置文件,咱們本身編寫的配置文件,也不能自動識別;
想讓Spring的配置文件生效,加載進來;@ImportResource標註在一個啓動Application配置類上,ApplicationContext.xml
<bean id="helloService" class="com.bdqn.Controller.service.HelloService"></bean>
單元測試類代碼以下,不加入@ImportResource狀況下
@Test public void TestService(){ //測試容器中是否有這個值 Boolean bean= (Boolean) ioc.containsBean("helloService"); System.out.println(bean); }
@SpringBootApplication @ComponentScan(basePackages = {"com.bdqn"}) //引入spring的配置文件 @ImportResource(locations = {"classpath:ApplicationContext.xml"}) /** * 當測試啓動類和Controller不位於同一個包下面時候須要 * 配置@ComponentScan(basePackages = {"com.bdqn.Controller"})去掃描controller的路徑 */ public class SpringBootQuickApplication { public static void main(String[] args) { SpringApplication.run(SpringBootQuickApplication.class, args); } }
不用編寫Spring的配置文件
SpringBoot推薦給容器中添加組件的方式;推薦使用全註解的方式,可是我以爲太麻煩了,每次建立一個bean都要建立一個類對象
一、配置類@Configuration------>Spring配置文件
二、使用@Bean給容器中添加組件
/** * @Configuration:指明當前類是一個配置類;就是來替代以前的Spring配置文件 * * 在配置文件中用<bean><bean/>標籤添加組件 * */ @Configuration public class MyAppConfig { //將方法的返回值添加到容器中;容器中這個組件默認的id就是方法名 @Bean public HelloService helloService02(){ System.out.println("配置類@Bean給容器中添加組件了..."); return new HelloService(); } }
一、隨機數
${random.value}、${random.int}、${random.long} ${random.int(10)}、${random.int[1024,65536]}
二、佔位符獲取以前配置的值,若是沒有能夠是用:指定默認值
person.last-name=張三${random.uuid} person.age=${random.int} person.birth=2017/12/15 person.boss=false person.maps.k1=v1 person.maps.k2=14 person.lists=a,b,c //前面有person.maps這個值,後面person.dog.name想用一下前面的值 person.dog.name=${person.maps:hello}_dog person.dog.age=15
是springboot用來作多環境支持的,開發環境、測試環境、運行環境
咱們在主配置文件編寫的時候,文件名能夠是 application-{profile}.properties/yml
默認使用application.properties的配置;
server: port: 8081 spring: profiles: active: prod ---//三個橫線+回車 server: port: 8083 spring: profiles: dev --- server: port: 8084 spring: profiles: prod #指定屬於哪一個環境
一、在application.properties配置文件中指定 spring.profiles.active=dev
二、命令行:
java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev;
能夠直接在測試的時候,配置傳入命令行參數
三、虛擬機參數;
-Dspring.profiles.active=dev
springboot 啓動會掃描如下位置的application.properties或者application.yml文件做爲Spring boot的默認配置文件
–file:./config/
–file:./
–classpath:/config/
–classpath:/
入圖上所展現,優先級由高到底,高優先級的配置會覆蓋低優先級的配置;
SpringBoot會從這四個位置所有加載主配置文件;互補配置;
==咱們還能夠經過spring.config.location來改變默認的配置文件位置==
項目打包好之後,咱們可使用命令行參數的形式,啓動項目的時候來指定配置文件的新位置;指定配置文件和默認加載的這些配置文件共同起做用造成互補配置;
先輸入 cd target
在輸出dir
輸入:java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --spring.config.location=F:/application.properties(裏面是湍口號內容)也能夠運行項目
==SpringBoot也能夠從如下位置加載配置; 優先級從高到低;高優先級的配置覆蓋低優先級的配置,全部的配置會造成互補配置==
1.命令行參數
全部的配置均可以在命令行上進行指定
java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --server.port=8087 --server.port=8087
--server.context-path=/abc //訪問項目名稱,可是須要修改多個配置的時候這麼寫的話?顯得很累贅
能夠專門寫一個外部配置文件
多個配置用空格分開; --配置項=值
2.來自java:comp/env的JNDI屬性
3.Java系統屬性(System.getProperties())
4.操做系統環境變量
5.RandomValuePropertySource配置的random.*屬性值
==由jar包外向jar包內進行尋找;==
==優先加載帶profile==
優先加載,外部jar包旁邊的配置文件
6.jar包外部的application-{profile}.properties或application.yml(帶spring.profile)配置文件**
7.jar包內部的application-{profile}.properties或application.yml(帶spring.profile)配置文件
==再來加載不帶profile==
8.jar包外部的application.properties或application.yml(不帶spring.profile)配置文件
9.jar包內部的application.properties或application.yml(不帶spring.profile)配置文件
10.@Configuration註解類上的@PropertySource
11.經過SpringApplication.setDefaultProperties指定的默認屬性
全部支持的配置加載來源;
配置文件到底能寫什麼?怎麼寫?自動配置原理;
1)、SpringBoot啓動的時候加載主配置類,開啓了自動配置功能 ==@EnableAutoConfiguration==
2)、@EnableAutoConfiguration 做用:
利用EnableAutoConfigurationImportSelector給容器中導入一些組件?
能夠查看selectImports()方法的內容;
List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);獲取候選的配置
SpringFactoriesLoader.loadFactoryNames() 掃描全部jar包類路徑下 META-INF/spring.factories 把掃描到的這些文件的內容包裝成properties對象 從properties中獲取到EnableAutoConfiguration.class類(類名)對應的值,而後把他們添加在容器中
==將 類路徑下 META-INF/spring.factories 裏面配置的全部EnableAutoConfiguration的值加入到了容器中;==
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\ org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\ org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\ org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\ org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\ org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\ org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\ org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\ org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\ org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\ org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\ org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.ldap.LdapDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\ org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\ org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\ org.springframework.boot.autoconfigure.elasticsearch.jest.JestAutoConfiguration,\ org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\ org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\ org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration,\ org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\ org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,\ org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,\ org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration,\ org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\ org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\ org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration,\ org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\ org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration,\ org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration,\ org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,\ org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\ org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\ org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\ org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\ org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\ org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration,\ org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration,\ org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\ org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,\ org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration,\ org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration,\ org.springframework.boot.autoconfigure.mobile.DeviceDelegatingViewResolverAutoConfiguration,\ org.springframework.boot.autoconfigure.mobile.SitePreferenceAutoConfiguration,\ org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration,\ org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\ org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\ org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\ org.springframework.boot.autoconfigure.reactor.ReactorAutoConfiguration,\ org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration,\ org.springframework.boot.autoconfigure.security.SecurityFilterAutoConfiguration,\ org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration,\ org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration,\ org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\ org.springframework.boot.autoconfigure.session.SessionAutoConfiguration,\ org.springframework.boot.autoconfigure.social.SocialWebAutoConfiguration,\ org.springframework.boot.autoconfigure.social.FacebookAutoConfiguration,\ org.springframework.boot.autoconfigure.social.LinkedInAutoConfiguration,\ org.springframework.boot.autoconfigure.social.TwitterAutoConfiguration,\ org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\ org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\ org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\ org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\ org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\ org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration,\ org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration,\ org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration,\ org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration,\ org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration,\ org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration,\ org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration,\ org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration,\ org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration,\ org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration,\ org.springframework.boot.autoconfigure.websocket.WebSocketMessagingAutoConfiguration,\ org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration
每個這樣的 xxxAutoConfiguration類都是容器中的一個組件,都加入到容器中;用他們來作自動配置;
3)、每個自動配置類進行自動配置功能;
4)、以HttpEncodingAutoConfiguration(Http編碼自動配置)爲例解釋自動配置原理;
@Configuration //表示這是一個配置類,之前編寫的配置文件同樣,也能夠給容器中添加組件 @EnableConfigurationProperties(HttpEncodingProperties.class) //啓動指定類的ConfigurationProperties功能;將配置文件中對應的值和HttpEncodingProperties綁定起來;並把HttpEncodingProperties加入到ioc容器中 @ConditionalOnWebApplication //Spring底層@Conditional註解(Spring註解版),根據不一樣的條件,若是知足指定的條件,整個配置類裏面的配置就會生效; 判斷當前應用是不是web應用,若是是,當前配置類生效 @ConditionalOnClass(CharacterEncodingFilter.class) //判斷當前項目有沒有這個類CharacterEncodingFilter;SpringMVC中進行亂碼解決的過濾器; @ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true) //判斷配置文件中是否存在某個配置 spring.http.encoding.enabled;若是不存在,判斷也是成立的 //即便咱們配置文件中不配置pring.http.encoding.enabled=true,也是默認生效的; public class HttpEncodingAutoConfiguration { //他已經和SpringBoot的配置文件映射了 private final HttpEncodingProperties properties; //只有一個有參構造器的狀況下,參數的值就會從容器中拿 public HttpEncodingAutoConfiguration(HttpEncodingProperties properties) { this.properties = properties; } @Bean //給容器中添加一個組件,這個組件的某些值須要從properties中獲取 @ConditionalOnMissingBean(CharacterEncodingFilter.class) //判斷容器沒有這個組件? public CharacterEncodingFilter characterEncodingFilter() { CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter(); filter.setEncoding(this.properties.getCharset().name()); filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST)); filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE)); return filter; }
根據當前不一樣的條件判斷,決定這個配置類是否生效? 一但這個配置類生效;這個配置類就會給容器中添加各類組件;這些組件的屬性是從對應的properties類中獲取的,這些類裏面的每個屬性又是和配置文件綁定的;
5)、全部在配置文件中能配置的屬性都是在xxxxProperties類中封裝;配置文件能配置什麼就能夠參照某個功能對應的這個屬性類
@ConfigurationProperties(prefix = "spring.http.encoding") //從配置文件中獲取指定的值和bean的屬性進行綁定 public class HttpEncodingProperties { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
精髓:springboot裏面的HttpEncodingProperties會自動配置好不少注組件
1)、SpringBoot啓動會加載大量的自動配置類
2)、咱們看咱們須要的功能有沒有SpringBoot默認寫好的自動配置類;
3)、咱們再來看這個自動配置類中到底配置了哪些組件;(只要咱們要用的組件有,咱們就不須要再來配置了)
4)、給容器中自動配置類添加組件的時候,會從properties類中獲取某些屬性。咱們就能夠在配置文件中指定這些屬性的值;
xxxxAutoConfigurartion:自動配置類;
給容器中添加組件
xxxxProperties:封裝配置文件中相關屬性;
做用:必須是@Conditional指定的條件成立,纔給容器中添加組件,配置配裏面的全部內容才生效;
自動配置類必須在必定的條件下才能生效;
咱們怎麼知道哪些自動配置類生效;
==咱們能夠經過啓用 在全局配置文件aplication.properties中開啓 debug=true屬性;來讓控制檯打印自動配置報告==,這樣咱們就能夠很方便的知道哪些自動配置類生效;
#開啓Springboot中的debug debug=true #配置端口號 server.port=8089 #咱們能配置那些都是來源於properties類 spring.http.encoding.enabled=true spring.http.encoding.charset=utf-8 spring.http.encoding.force=true
========================= AUTO-CONFIGURATION REPORT ========================= Positive matches:(自動配置類啓用的) ----------------- DispatcherServletAutoConfiguration matched: - @ConditionalOnClass found required class 'org.springframework.web.servlet.DispatcherServlet'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition) - @ConditionalOnWebApplication (required) found StandardServletEnvironment (OnWebApplicationCondition) Negative matches:(沒有啓動,沒有匹配成功的自動配置類) ----------------- ActiveMQAutoConfiguration: Did not match: - @ConditionalOnClass did not find required classes 'javax.jms.ConnectionFactory', 'org.apache.activemq.ActiveMQConnectionFactory' (OnClassCondition) AopAutoConfiguration: Did not match: - @ConditionalOnClass did not find required classes 'org.aspectj.lang.annotation.Aspect', 'org.aspectj.lang.reflect.Advice' (OnClassCondition)