spring-boot -配置文件值注入


/**
 * 將配置文件中配置的每一個屬性的值,映射到這個組件中
 * @ConfigurationProperties:告訴SpringBoot將本類中的所有屬性和配置文件中相關的配置進行綁定;
  默認在全局配置文件中獲取值的 * prefix = "person":配置文件中哪個下面的所有屬性進行一一映射 * * 只有這個組件是容器中的組件,才能容器提供的@ConfigurationProperties功能;
@Component
* */

 

配置文件內容:

 

person:
    lastName: hello
    age: 18
    boss: false
    birth: 2017/12/12
    maps: {k1: v1,k2: 12}
    lists:
      - lisi
      - zhaoliu
    dog:
      name: 小狗
      age: 12

映射:

@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;

 

除了使用 @ConfigurationProperties進行配置值獲取

之前使用bean標簽添加到容器中的時候,對應現在的就是@Component

value標簽對應的就是現在注解@Value

 

 

 松散語法:

 

 

 

配置文件yml還是properties他們都能獲取到值;

如果說,我們只是在某個業務邏輯中需要獲取一下配置文件中的某項值,使用@Value;

如果說,我們專門編寫了一個javaBean來和配置文件進行映射,我們就直接使用@ConfigurationProperties
@Component
@Validated//表示里面的value需要進行JSR303數據校驗,就是下面的@Email
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;

 

@PropertySource&@ImportResource&@Bean

/**
 * 將配置文件中配置的每一個屬性的值,映射到這個組件中
 * @ConfigurationProperties:告訴SpringBoot將本類中的所有屬性和配置文件中相關的配置進行綁定;
 *      prefix = "person":配置文件中哪個下面的所有屬性進行一一映射
 *
 * 只有這個組件是容器中的組件,才能容器提供的@ConfigurationProperties功能;
 *  @ConfigurationProperties(prefix = "person")默認從全局配置文件中獲取值;
 *
 */

 

例子:

@PropertySource(value = {"classpath:person.properties"})//classpath表示的是main路徑下的
@Component
@ConfigurationProperties(prefix = "person")
//@Validated
public class Person {

 

 

@ImportResource:導入Spring的配置文件,讓配置文件里面的內容生效

Spring Boot里面沒有Spring的配置文件,我們自己編寫的配置文件,也不能自動識別;

想讓Spring的配置文件生效,加載進來;@ImportResource標注在一個配置類上

@ImportResource(locations = {"classpath:beans.xml"})
導入Spring的配置文件讓其生效

 

以前需要編寫spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="helloService" class="com.atguigu.springboot.service.HelloService"></bean>
</beans>

 

現在:

SpringBoot推薦給容器中添加組件的方式;推薦使用全注解的方式

 

 

 

 

1、配置類@Configuration------>Spring配置文件

2、使用@Bean給容器中添加組件

/**
 * @Configuration:指明當前類是一個配置類;就是來替代之前的Spring配置文件
 *
 * 在配置文件中用<bean><bean/>標簽添加組件
 *
 */
@Configuration
public class MyAppConfig {

    //將方法的返回值添加到容器中;容器中這個組件默認的id就是方法名
    @Bean
    public HelloService helloService02(){
        System.out.println("配置類@Bean給容器中添加組件了...");
        return new HelloService();
    }
}

 

 

 

多Profile文件

我們在主配置文件編寫的時候,文件名可以是 application-{profile}.properties/yml

默認使用application.properties的配置;

編寫不同環境的配置文件:
application-dev.properties

在默認的全局配置文件當中
即application.properties中配置:
在配置文件中指定  spring.profiles.active=dev

 

yml支持多文檔塊方式

server:
  port: 8081
spring:
  profiles:
    active: prod

---
server:
  port: 8083
spring:
  profiles: dev


---

server:
  port: 8084
spring:
  profiles: prod  #指定屬於哪個環境

---三個橫線就是代表不同的配置內容分塊,active:就是要激活的配置塊

 

 

 

 

 

 SpringBoot會從這四個位置全部加載主配置文件;互補配置

 

**項目打包好以后,我們可以使用命令行參數的形式,啟動項目的時候來指定配置文件的新位置;
指定配置文件和默認加載的這些配置文件共同起作用形成互補配置;** java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar
--spring.config.location=G:/application.properties

 

 

 

 

jar --jfijeifjie  --server.port= 8089 命令行

 

自動配置原理

SpringBoot啟動的時候加載主配置類,開啟了自動配置功能 ==@EnableAutoConfiguration==

 

 

 @EnableAutoConfiguration 作用:

 

 

 通過selectImport方法進行選擇加載

        public Iterable<Entry> selectImports() {
            if (this.autoConfigurationEntries.isEmpty()) {
                return Collections.emptyList();
            } else {
                Set<String> allExclusions = (Set)this.autoConfigurationEntries.stream().map(AutoConfigurationImportSelector.AutoConfigurationEntry::getExclusions).flatMap(Collection::stream).collect(Collectors.toSet());
                Set<String> processedConfigurations = (Set)this.autoConfigurationEntries.stream().map(AutoConfigurationImportSelector.AutoConfigurationEntry::getConfigurations).flatMap(Collection::stream).collect(Collectors.toCollection(LinkedHashSet::new));
                processedConfigurations.removeAll(allExclusions);
                return (Iterable)this.sortAutoConfigurations(processedConfigurations, this.getAutoConfigurationMetadata()).stream().map((importClassName) -> {
                    return new Entry((AnnotationMetadata)this.entries.get(importClassName), importClassName);
                }).collect(Collectors.toList());
            }
        }

 

List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);獲取候選的配置

    protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
        Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
        return configurations;
    }
SpringFactoriesLoader.loadFactoryNames()
掃描所有jar包類路徑下  META-INF/spring.factories
把掃描到的這些文件的內容包裝成properties對象
從properties中獲取到EnableAutoConfiguration.class類(類名)對應的值,然后把他們添加在容器中

==將 類路徑下 META-INF/spring.factories 里面配置的所有EnableAutoConfiguration的值加入到了容器中;==

# PropertySource Loaders
org.springframework.boot.env.PropertySourceLoader=\
org.springframework.boot.env.PropertiesPropertySourceLoader,\
org.springframework.boot.env.YamlPropertySourceLoader

# Run Listeners
org.springframework.boot.SpringApplicationRunListener=\
org.springframework.boot.context.event.EventPublishingRunListener

# Error Reporters
org.springframework.boot.SpringBootExceptionReporter=\
org.springframework.boot.diagnostics.FailureAnalyzers

# Application Context Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\
org.springframework.boot.context.ContextIdApplicationContextInitializer,\
org.springframework.boot.context.config.DelegatingApplicationContextInitializer,\
org.springframework.boot.rsocket.context.RSocketPortInfoApplicationContextInitializer,\
org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer

# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.ClearCachesApplicationListener,\
org.springframework.boot.builder.ParentContextCloserApplicationListener,\
org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor,\
org.springframework.boot.context.FileEncodingApplicationListener,\
org.springframework.boot.context.config.AnsiOutputApplicationListener,\
org.springframework.boot.context.config.ConfigFileApplicationListener,\
org.springframework.boot.context.config.DelegatingApplicationListener,\
org.springframework.boot.context.logging.ClasspathLoggingApplicationListener,\
org.springframework.boot.context.logging.LoggingApplicationListener,\
org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener

# Environment Post Processors
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor,\
org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor,\
org.springframework.boot.env.SystemEnvironmentPropertySourceEnvironmentPostProcessor,\
org.springframework.boot.reactor.DebugAgentEnvironmentPostProcessor

# Failure Analyzers
org.springframework.boot.diagnostics.FailureAnalyzer=\
org.springframework.boot.diagnostics.analyzer.BeanCurrentlyInCreationFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.BeanDefinitionOverrideFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.BeanNotOfRequiredTypeFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.BindFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.BindValidationFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.UnboundConfigurationPropertyFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.ConnectorStartFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.NoSuchMethodFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.NoUniqueBeanDefinitionFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.PortInUseFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.ValidationExceptionFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.InvalidConfigurationPropertyNameFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.InvalidConfigurationPropertyValueFailureAnalyzer

# FailureAnalysisReporters
org.springframework.boot.diagnostics.FailureAnalysisReporter=\
org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter

每一個這樣的 xxxAutoConfiguration類都是容器中的一個組件,都加入到容器中;用他們來做自動配置;

 

每一個自動配置類進行自動配置功能;

 

HttpEncodingAutoConfiguration(Http編碼自動配置)為例解釋自動配置原理;

 

//表示這是一個配置類,以前編寫的配置文件一樣,也可以給容器中添加組件
@Configuration(
    proxyBeanMethods = false
)

 //啟動指定類的ConfigurationProperties功能;將配置文件中對應的值和HttpEncodingProperties綁定起來;
並把HttpEncodingProperties加入到ioc容器中
@EnableConfigurationProperties({ServerProperties.class}) /Spring底層@Conditional注解(Spring注解版),根據不同的條件,如果滿足指定的條件,整個配置類里面的配置
就會生效; 判斷當前應用是否是web應用,如果是,當前配置類生效 @ConditionalOnWebApplication( type
= Type.SERVLET ) //判斷當前項目有沒有這個類CharacterEncodingFilter;SpringMVC中進行亂碼解決的過濾器; @ConditionalOnClass({CharacterEncodingFilter.class}) //判斷配置文件中是否存在某個配置 spring.http.encoding.enabled;如果不存在,判斷也是成立的 //即使我們配置文件中不配置pring.http.encoding.enabled=true,也是默認生效的; @ConditionalOnProperty( prefix = "server.servlet.encoding", value = {"enabled"}, matchIfMissing = true ) public class HttpEncodingAutoConfiguration { //他已經和SpringBoot的配置文件映射了 private final Encoding properties; //只有一個有參構造器的情況下,參數的值就會從容器中拿 public HttpEncodingAutoConfiguration(ServerProperties properties) { this.properties = properties.getServlet().getEncoding(); } @Bean//給容器中添加一個組件,這個組件的某些值需要從properties中獲取 //判斷容器沒有這個組件? @ConditionalOnMissingBean public CharacterEncodingFilter characterEncodingFilter() { CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter(); filter.setEncoding(this.properties.getCharset().name()); filter.setForceRequestEncoding(this.properties.shouldForce(org.springframework.boot.web.servlet.server.Encoding.Type.REQUEST)); filter.setForceResponseEncoding(this.properties.shouldForce(org.springframework.boot.web.servlet.server.Encoding.Type.RESPONSE)); return filter; }

根據當前不同的條件判斷,決定這個配置類是否生效?

一但這個配置類生效;這個配置類就會給容器中添加各種組件;這些組件的屬性是從對應的properties類中獲取的,這些類里面的每一個屬性又是和配置文件綁定的;

 

 

所有在配置文件中能配置的屬性都是在xxxxProperties類中封裝者‘;配置文件能配置什么就可以參照某個

功能對應的這個屬性類

 

 

 

 

 

 

 

@Conditional派生注解(Spring注解版原生的@Conditional作用)

作用:必須是@Conditional指定的條件成立,才給容器中添加組件,配置配里面的所有內容才生效;

 

 

 自動配置類必須在一定的條件下才能生效;

 

==我們可以通過啟用 debug=true屬性;來讓控制台打印自動配置報告==,這樣我們就可以很方便的知道哪些自動配置類生效;

============================
CONDITIONS EVALUATION REPORT
============================


Positive matches:   //自動加載啟用的配置累
-----------------

   AopAutoConfiguration matched:
      - @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)

   AopAutoConfiguration.ClassProxyingConfiguration matched:
      - @ConditionalOnMissingClass did not find unwanted class 'org.aspectj.weaver.Advice' (OnClassCondition)
      - @ConditionalOnProperty (spring.aop.proxy-target-class=true) matched (OnPropertyCondition)

   DispatcherServletAutoConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.web




Negative matches:自動加載不啟用,不滿足condition條件的
-----------------

   ActiveMQAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)

   AopAutoConfiguration.AspectJAutoProxyingConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'org.aspectj.weaver.Advice' (OnClassCondition)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM