2.Spring Boot 配置文件

2.配置文件

2.1 配置文件

SpringBoot使用一个全局的配置文件,配置文件名是固定的

  • application.properties
  • application.yml

配置文件的作用
修改SpringBoot的自动配置的默认值;

2.2 YAML

2.2.1 YAML简介

简介

YAML(YAML Ain’t Markup Language)同JSON和XML是一门标记性语言,但是以数据为中心的YAML比JSON、XML更适合作为配置文件

YML例子

server:
	port: 8080

xml例子

<server> 
	<port>8081</port>      
</server>

2.2.2 YAML语法

1.基本语法
  • k:(空格)v:表示一对键值对(空格必须有);
  • 以空格的缩进来控制层级的关系;只要是左对齐的一列数据,都是同一层级的
server:
	port: 8080
	path: /hello

注意
属性和值也是大小写敏感的;

2.值的写法

字面量

  • k: v:字面量直接来写
 字符串默认不用加上单引号或双引号

“”:双引号;不会转义字符串里面的特殊字符串;特殊字符串会作为本身想表示的意思

name:"zhangsan \n lisi" 输出;zhangsan换行lisi

‘’:单引号会转义特殊字符,特殊字符串最终只是一个普通字符串数据

name:'zhangsan \n lisi' 输出 zhangsan \n lisi

对象、Map(属性和值)(键值对)

k: v:在下一行来对象的属性和值的关系;

  • 换行写法
friends:
	lastName: zhangsan
	age: 20
  • 行内写法
firends:{lastName: zhangsan,age: 18}

数组(List,Set)
用 - 值表示数组中的一个元素

换行写法

pets:
	- cat
	- dog
	- pig
	

行内写法

pets: [cat,dog,pig]
3. 配置文件值的注入

配置文件(yml)

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

注意
properties 中的编码格式默认是ASK码
配置文件(properties)

person.last-name=hello
person.age=18
person.birth=2017/12/12
person.boss=false
person.maps.k1=v1
person.maps.k2=l2
person.lists=lisi,zhaoliu
person.dog.name=小狗
person.dog.age=12

javaBean

/**
 * 将配置文件中配置的每一个属性的值,映射到这个组件中
 * @ConfigurationProperties 告诉将本类中的所有属性和配置文件中相关的配置进行绑定
 *  prefix="person" 配置文件中哪个下所有属性进行一一映射
 *  只有这个组件是容器中的组件,才能使用容器提供的功能
 */
@Component
@ConfigurationProperties("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;

测试

/**
 * SpringBoot的单元测试
 * 在测试期间很方便进行注入
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02ConfigApplicationTests {
    @Autowired
    Person person;

    @Test
    public void contextLoads() {
        System.out.println(person);
    }
}
3.1 配置文件处理器

可以导入配置文件处理器,以后编写配置文件就有提示了

<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
   <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
    </dependency>

提示

3.2 @Value和@ConofigrationProperties的区别

取值上的区别

@ConfigurationProperties@Value
功能批量注入配置文件中的属性一个个指定
松散绑定支持不支持
SpEL不支持支持
JSR303数据校验支持不支持
复杂类型封装支持不支持

引用场景的区别

  • 只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@value
  • 专门编写了一个javaBean来和配置文件进行配置映射,就直接使用@ConfigurationProperties;
3.3 配置文件注入值数据校验

javaBean

@Component
@Validated
@ConfigurationProperties("person")
public class person {
    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;
    @Email
    private String email;
    private Map<String, Object> maps;
    private List<Object> lists;
    private Dog dog;

故意写错的配置文件

person:
  email: 12222
  ...

异常

Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'person' to springbootstudy.springboot02config.entity.person failed:

    Property: person.email
    Value: 12222
    Origin: class path resource [application.yml]:13:10
    Reason: 不是一个合法的电子邮件地址
4. @PropertySource&@ImportResource@Bean
4.1@PropertySource

@ConfigurationProperties 默认是从全局配置文件中获取值,@PropetySource则设置@ConfigurationProperties的配置文件加载路径。

注意
@PropetySource只能指定*.properties文件

@Component
@PropertySource("classpath:Person.properties")
@ConfigurationProperties("person")
public class person {
    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;
    private String email;
    private Map<String, Object> maps;
    private List<Object> lists;
    private Dog dog;

4.2 @ImportResource

导入Spring的配置文件,让配置文件里面的内容生效;
SpringBoot里面是没有Spring的配置文件,我们自己编写的配置文件也不能识别;
想让Spring的配置文件生效,加载进来,需要使用@ImportResource将配置文件加载进来

@SpringBootApplication
@ImportResource(locations = "classpath:bean.xml")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
<?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 class="springbootstudy.springboot02config.service.HelloService"></bean>
</beans>
4.3全注解方式添加组件

SpringBoot推荐给容器添加组件的方式,推荐使用全注解的方式

  • 1.配置@Configuration:指明当前类是一个配置类;就是来替代之前Spring配置文件
  • 2.使用@Bean给容器添加组件; 将方法的返回值添加到容器中,容器中这个组件默认的ID就是方法名。
@Configuration
public class MyAppConfig {
    @Bean
    public HelloService helloService() {
        System.out.println("配置类@Bean给容器添加组件了....");
        return new HelloService();
    }
}
4. 配置文件占位符
4.1 随机数
${random.value}、${random.int}、${random.long}
${random.int(10)}、${random.int[1024,65536]}、${random.uuid}
4.2 占位符默认值值

在占位符获取之前配置的值,如果没有可以用“:”指定默认值

person:
  last-name: 张三${random.uuid}
  age: ${random.int}
  boss: false
  birth: 2017/12/12
  maps: {k1: v1,k2: v2}
  lists:
    - lisi
    - zhaoliu
  dog:
    name: ${prson.hello:小王}的小狗
    age: 12
5.Profile
5.1多Profile文件

在编写主配置文件的时候,文件名可以是application-{profile}.properties/yml;
默认使用application.properties/yml的配置,在主配置文件中可以指定profile配置
yml

spring:
  profiles:
    active: dev

properties

spring.profiles.active=prod
5.2 yml支持多文档块的方式

在yml中可以使用“—” 来分割文档

server:
  port: 8081
spring:
  profiles:
    active: dev
---
server:
  port: 8082
spring:
  profiles: dev
---
server:
  port: 8083
spring:
  profiles: prod

如果使用yml的多文档块的形式,则需要在第一个文档中指定profile

5.3激活指定profile

激活的几种方式

配置文件中指定

在配置文件中指定 spring.profiles.active=dev

server:
  port: 8081
spring:
  profiles:
    active: dev
---
server:
  port: 8082
spring:
  profiles: dev
---
server:
  port: 8083
spring:
  profiles: prod

命令行

java -jar spring-boot-SNAPSHOT.jar --spring.profiles.active=dev

虚拟机参数

-Dspring.profiles.active=dev
6.配置文件加载位置

springboot 启动会扫描一下位置的application.properties或者application.yml作为spring boot的默认配置文件

  • file:/config/
  • file:/
  • classpath:config/
  • classpath:/

优先级由高到底,高优先级的配置会覆盖低优先级的配置;

SpringBoot会从这个四个位置全部加载主配置文件,形成 互补配置

示例

文件加载优先级
文件的内容如下

classpath:

server.port=8081

classpath:config

server.port=8082
server.servlet.context-path=/hello

file:

server.port=8083

file:config

server.port=8084

那么项目的访问路径就会变成

 localhost:8084/hello

除此之外在项目打包完成后还可以通过 spring.config.location 来改变默认的配置文件位置,同时该文件会和默认加载的文件共同起作用。

java -jar spring-boot.jar --spring.config.location=E:\StudyProject\SpringBootStudy\application.yml
7.外部配置加载顺序
7.1常用外部配置加载顺序

SpringBoot也可以从以下位置加载配置; 优先级从高到低;高优先级的配置覆盖低优先级的配置,如果有存在不同,所有的配置会 形成互补配置。

1.命令行参数,所有的配置都可以在命令行上进行指定

2.来自java:comp/env** 的JDNI属性

3.java系统属性(System.getProperties)

4.操作系统环境变量

5.RandomValuePropertySource配置的random.*属性值

由jar包外向jar包内进行寻找;

优先加载带profiles;

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.@ConfigurationProperties注解上的@PropertySource

11.通过SpringApplication.setDefaultProperties指定的默认属性

cmd示例

java -jar spring-boot.jar --spring.config.location=E:\StudyProject\SpringBootStudy\application.yml --server.port=8087 --server.servlet.context-path=/test

注意
多个配置用空格隔开:- -配置项=值

配置示例
外部加载顺序
上面示例会先加载 file:/applicationproperties

8.自动配置原理
8.1 自动配置原理
  1. SpringBoot启动的时候加载主配置类,开启了自动配置功能 @EnableAutoConfigruation
  2. @EnableAutoConfiguration作用
    • 利用EnableAutoConfigurationImportSelector给容器导入一些组件。
    • 具体实现查看:org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#selectImports 方法,大概流程如下
    selectImports 方法扫描所有jar包类路径下 ' META‐INF/spring.factories ';
    
    把扫描到的这些文件的内容包装成properties对象;
    
    从properties对象中获取到EnableAutoConfiguration.class类(类名)对应的值,然后把他们添加在容器中 ;
    
    #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.CloudServiceConnectorsAutoConfiguration,\
    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.CassandraReactiveDataAutoConfiguration,\
    org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveRepositoriesAutoConfiguration,\
    org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\
    org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\
    org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveDataAutoConfiguration,\
    org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveRepositoriesAutoConfiguration,\
    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.jdbc.JdbcRepositoriesAutoConfiguration,\
    org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
    org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\
    org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\
    org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration,\
    org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoConfiguration,\
    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.RedisReactiveAutoConfiguration,\
    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.elasticsearch.rest.RestClientAutoConfiguration,\
    org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
    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.http.HttpMessageConvertersAutoConfiguration,\
    org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration,\
    org.springframework.boot.autoconfigure.influx.InfluxDbAutoConfiguration,\
    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.groovy.template.GroovyTemplateAutoConfiguration,\
    org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\
    org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\
    org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,\
    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.mongo.embedded.EmbeddedMongoAutoConfiguration,\
    org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
    org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration,\
    org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
    org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
    org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration,\
    org.springframework.boot.autoconfigure.reactor.core.ReactorCoreAutoConfiguration,\
    org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\
    org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration,\
    org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration,\
    org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration,\
    org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration,\
    org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\
    org.springframework.boot.autoconfigure.session.SessionAutoConfiguration,\
    org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration,\
    org.springframework.boot.autoconfigure.security.oauth2.client.reactive.ReactiveOAuth2ClientAutoConfiguration,\
    org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration,\
    org.springframework.boot.autoconfigure.security.oauth2.resource.reactive.ReactiveOAuth2ResourceServerAutoConfiguration,\
    org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\
    org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration,\
    org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration,\
    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.client.RestTemplateAutoConfiguration,\
    org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration,\
    org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration,\
    org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryAutoConfiguration,\
    org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration,\
    org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration,\
    org.springframework.boot.autoconfigure.web.reactive.function.client.ClientHttpConnectorAutoConfiguration,\
    org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration,\
    org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,\
    org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\
    org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration,\
    org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\
    org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\
    org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\
    org.springframework.boot.autoconfigure.websocket.reactive.WebSocketReactiveAutoConfiguration,\
    org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration,\
    org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration,\
    org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration,\
    org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAutoConfiguration
    
  3. HttpEncodingAutoConfiguration
@Configuration // 这是一个配置类,和之前的配置文件一样,也可以给容器添加组件
@EnableConfigurationProperties(HttpProperties.class)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnClass(CharacterEncodingFilter.class)
@ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true)
public class HttpEncodingAutoConfiguration {

@EnableConfigurationProperties

启用指定类的ConfigurationProperties功能,将配置文件中对应的值和HttpProperties属性绑定起来,并加入容器
所有的配置文件中的属性都是xxxProperties类中封装着,配置文件能配置什么都可以参照某个功能对应的这个类属性;
@ConfigurationProperties(prefix = "spring.http")// 从配置文件中获取指定的值和bean的属性
public class HttpProperties {

@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)

SpringBoot底层@Conditional注解,根据不同的条件,如果满足指定的条件,配置类里面的配置就会生效@ConditionalOnWebApplication 判断当前应用是否是web应用,如果是当前配置类生效。

@ConditionalOnClass(CharacterEncodingFilter.class)

判断当前项目有没有这个类CharacterEncodingFilter;

@ConditionalOnProperty(prefix = “spring.http.encoding”, value = “enabled”, matchIfMissing = true)

判断配置文件中是否存在某个配置 spring.http.encoding,如果不出存在也是成立的,及时我们在配置文件中没有配置spring.http.encoding也是默认生效的。

根据当前不同的条件判断,决定这个配置类是否生效;

在条件都符合的情况下HttpEncodingAutoConfiguration 会往容器中注入值

	// 已经和SpringBoot的配置文件绑定了
	private final HttpProperties.Encoding properties;
	
	// 只有一个有参构造器情况下,参数的值就会冲容器中拿
	public HttpEncodingAutoConfiguration(HttpProperties properties) {
		this.properties = properties.getEncoding();
	} 

	@Bean // 给容器中添加一个组件,这个组件的默写值需要从properties中取
	@ConditionalOnMissingBean
	public CharacterEncodingFilter characterEncodingFilter() {
		CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
		filter.setEncoding(this.properties.getCharset().name());
		filter.setForceRequestEncoding(this.properties. (Type.REQUEST));
		filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE));
		return filter;
	}
9.@Conditional扩展注解和自动配置报告
9.1 @Conditional扩展注解

作用:必须是@Conditional指定的条件成立,才给容器中添加组件,配置文件里面的内容才生效。

@Conditional扩展注解作用(判断是否满足当前指定的条件
@ConditionalOnJava系统的java版本是否符合要求
@ConditonalOnBean容器中存在指定的Bean
@ConditionalOnMissingBean容器中不存在自定的Bean
@ConditionalOnExpression满足SpEL表达式的指定
@ConditionalOnClass系统中有指定的类
@ConditionalOnMissingClass系统中没有指定的类
@ConditionalOnSingleCandiate容器中只有一个指定的Bean,或者这个Bean是首选Bean
@ConditionalOnProperty系统中指定的属性是否有存在的值
@ConditionalOnResource类路径下是否存在指定资源文件
@ConditionalOnWebApplication当前是Web环境
@ConditionalOnNotWebApplication当前不是Web环境
@ConditionalOnjndlJNDI指定的项

自动配置类只有在一定的条件下才会生效;

9.2 自动配置报告
# 开启SpringBoot的DEBUG模式
debug=true

开发者可以通过设置 debug=true属性;来让控制台打印自动配置报告,这样可以很方便的知道有哪些自动配置生效,哪些自动配置不生效

Positive matches(自动配置类启用的):
-----------------

   CodecsAutoConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.http.codec.CodecConfigurer' (OnClassCondition)
      ...
Negative matches(没有启动,没有匹配成功的自动配置类):
-----------------

   ActiveMQAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)
         ...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值