1、新建一个项目中需要提供配置类

2、在META-INF/spring.factorties在文件中配置

  org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

  第三方jar中提供配置类全路径

实例演示:

bean-core工程:

package com.boot.config.core.domain;

public class Order {
}
package com.boot.config.core.domain;

public class Product {
}
package com.boot.config.core.config;

import com.boot.config.core.domain.Order;
import com.boot.config.core.domain.Product;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class BeanConfiguration { @Bean
public Order createOrder() {
return new Order();
} @Bean
public Product createProduct() {
return new Product();
}
}

spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.boot.config.core.config.BeanConfiguration

config-core工程:

package com.boot.config.core;

/**
* 数据源的属性类
*/
public class DatasourceProperties { private String driverClassName;
private String url;
private String username;
private String password; public String getDriverClassName() {
return driverClassName;
} public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
} public String getUrl() {
return url;
} public void setUrl(String url) {
this.url = url;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
}
package com.boot.config.core;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class DataSourceConfig { @Bean
@ConfigurationProperties(prefix = "jdbc")
public DatasourceProperties createDatasource() {
return new DatasourceProperties();
}
}

spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.boot.config.core.DataSourceConfig

boot-auto工程

Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.boot.auto.config</groupId>
<artifactId>boot-auto</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>boot-auto</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<!--引入三方依赖-->
<dependency>
<groupId>com.boot.config.core</groupId>
<artifactId>bean-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency> <dependency>
<groupId>com.boot.demo</groupId>
<artifactId>config-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--gson是springboot中装配的对象-->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

application.properties

jdbc.driverClassName = com.mysql.jdbc.Driver
#spring.boot.enableautoconfiguration = false 关闭配置功能,默认为true
package com.boot.auto.config.bootauto;

import com.boot.config.core.DatasourceProperties;
import com.boot.config.core.domain.Order;
import com.boot.config.core.domain.Product;
import com.google.gson.Gson;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext; // 这种方式只能排除配置类
// @EnableAutoConfiguration(exclude = {BeanConfiguration.class, DataSourceConfig.class})
// @EnableAutoConfiguration(excludeName = "com.boot.config.core.config.BeanConfiguration")
// @ComponentScan
@SpringBootApplication
// 注意点: exclude和excludeName 排除的类必须是配置类
public class BootAutoApplication { public static void main(String[] args) {
ConfigurableApplicationContext context =
SpringApplication.run(BootAutoApplication.class, args);
System.out.println(context.getBean(Order.class));
System.out.println(context.getBean(Product.class));
System.out.println(context.getBean(DatasourceProperties.class).getDriverClassName());
System.out.println(context.getBean("gson", Gson.class));
context.close();
}
}

打印结果

可以见得,我们通过配置自动将类装配到了spring容器中

查看spring自带配置gson的配置类GsonAutoConfiguration源码如下:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package org.springframework.boot.autoconfigure.gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered; @Configuration
@ConditionalOnClass({Gson.class})
@EnableConfigurationProperties({GsonProperties.class})
public class GsonAutoConfiguration {
public GsonAutoConfiguration() {
} @Bean
@ConditionalOnMissingBean
public GsonBuilder gsonBuilder(List<GsonBuilderCustomizer> customizers) {
GsonBuilder builder = new GsonBuilder();
customizers.forEach((c) -> {
c.customize(builder);
});
return builder;
} @Bean
@ConditionalOnMissingBean
public Gson gson(GsonBuilder gsonBuilder) {
return gsonBuilder.create();
} @Bean
public GsonAutoConfiguration.StandardGsonBuilderCustomizer standardGsonBuilderCustomizer(GsonProperties gsonProperties) {
return new GsonAutoConfiguration.StandardGsonBuilderCustomizer(gsonProperties);
} private static final class StandardGsonBuilderCustomizer implements GsonBuilderCustomizer, Ordered {
private final GsonProperties properties; StandardGsonBuilderCustomizer(GsonProperties properties) {
this.properties = properties;
} public int getOrder() {
return 0;
} public void customize(GsonBuilder builder) {
GsonProperties properties = this.properties;
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
properties.getClass();
map.from(properties::getGenerateNonExecutableJson).toCall(builder::generateNonExecutableJson);
properties.getClass();
map.from(properties::getExcludeFieldsWithoutExposeAnnotation).toCall(builder::excludeFieldsWithoutExposeAnnotation);
properties.getClass();
map.from(properties::getSerializeNulls).toCall(builder::serializeNulls);
properties.getClass();
map.from(properties::getEnableComplexMapKeySerialization).toCall(builder::enableComplexMapKeySerialization);
properties.getClass();
map.from(properties::getDisableInnerClassSerialization).toCall(builder::disableInnerClassSerialization);
properties.getClass();
map.from(properties::getLongSerializationPolicy).to(builder::setLongSerializationPolicy);
properties.getClass();
map.from(properties::getFieldNamingPolicy).to(builder::setFieldNamingPolicy);
properties.getClass();
map.from(properties::getPrettyPrinting).toCall(builder::setPrettyPrinting);
properties.getClass();
map.from(properties::getLenient).toCall(builder::setLenient);
properties.getClass();
map.from(properties::getDisableHtmlEscaping).toCall(builder::disableHtmlEscaping);
properties.getClass();
map.from(properties::getDateFormat).to(builder::setDateFormat);
}
}
}

对其中的主要两个注解进行解释:

springBoot @EnableAutoConfiguration深入分析的更多相关文章

  1. springboot EnableAutoConfiguration

    http://blog.javachen.com/2016/02/19/spring-boot-auto-configuration.html 自动配置 在启动类上使用@EnableAutoConfi ...

  2. SpringBoot @EnableAutoConfiguration exclude属性失效

    本文链接:https://blog.csdn.net/yuan_ren_sheng/article/details/81516779 在学习SpringBoot的时候,入了不少的坑.今天学习@Spri ...

  3. 008-Spring Boot @EnableAutoConfiguration深入分析、内部如何使用EnableAutoConfiguration

    一.EnableAutoConfiguration 1.EnableAutoConfiguration原理 springboot程序入口使用注解@SpringBootApplication,Sprin ...

  4. EnableAutoConfiguration注解 Spring中@Import注解的作用和使用

    EnableAutoConfiguration注解 http://www.51gjie.com/javaweb/1046.html springboot@EnableAutoConfiguration ...

  5. 001-spring boot概述与课程概要

    一.Spring Boot介绍 Spring Boot的目的在于创建和启动新的基于spring框架的项目.Spring boot会选择最适合的Spring 子项目和第三方开源库进行整合.大部分Spri ...

  6. spring源码:学习线索(li)

    一.spring xml配置(不包括AOP,主要了解在初始化及实例化过程中spring配置文件中每项内容的具体实现过程,从根本上掌握spring) <bean>的名字 &,alia ...

  7. spring源码:学习线索

    一.spring xml配置(不包括AOP,主要了解在初始化及实例化过程中spring配置文件中每项内容的具体实现过程,从根本上掌握spring) <bean>的名字 &,alia ...

  8. Spring Boot 实战与原理分析视频课程

    Spring Boot 实战与原理分析视频课程 链接:https://pan.baidu.com/share/init?surl=PeykcoeqZtd1d9lN9V_F-A 提取码: 关注公众号[G ...

  9. Spring Boot开启Druid数据库监控功能

    Druid是一个关系型数据库连接池,它是阿里巴巴的一个开源项目.Druid支持所有JDBC兼容的数据库,包括Oracle.MySQL.Derby.PostgreSQL.SQL Server.H2等.D ...

随机推荐

  1. ov5640介绍

    1 摄像头 在各类信息中,图像含有最丰富的信息,作为机器视觉领域的核心部件,摄像头被广泛地应用在安防.探险以及车牌检测等场合.摄像头按输出信号的类型来看可以分为数字摄像头和模拟摄像头,按照摄像头图像传 ...

  2. js polyfill , to developing the cross browser js

    https://github.com/paulmillr/console-polyfill https://github.com/Modernizr/Modernizr/wiki/HTML5-Cros ...

  3. 线上CPU飚高(死循环,死锁...)

    之前排除服务器内存暴增的问题,在此看到一篇类似的文章,做个类似的记录. 1.top基本使用 top 命令运行图: 第一行:基本信息 第二行:任务信息 第三行:CPU使用情况 第四行:物理内存使用情况 ...

  4. 关于DataTable.Select方法偶尔无法正确查到数据的处理方法

    项目中经常用DataTable在内存中存储并操作数据,在进行报表开发的时候,报表的各种过滤功能用这个内存表可以大现身手,但最近在使用过程中却遇到一个奇怪的现象,现将该问题及处理方法记录一下.这是在做护 ...

  5. python删除文本中的所有空字符

    import re import os input_path = 'G:/test/aa.json' output_path ='G:/test/bb.json' with open(input_pa ...

  6. lesson 20 pioneer pilots

    lesson 20 Pioneer pilots driver pilot rider cyclist 骑自行车的人 介词后不加that cover + 距离 = travel 了一段距离 by su ...

  7. centos7安装zabbix3.2详解

    服务器端安装 1.安装仓库 rpm -ivh http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noar ...

  8. C++ 学习笔记之——STL 库 vector

    vector 是一种顺序容器,可以看作是可以改变大小的数组. 就像数组一样,vector 占用连续的内存地址来存储元素,因此可以像数组一样用偏移量来随机访问,但是它的大小可以动态改变,容器会自动处理内 ...

  9. SpringBoot在IDEA下使用JPA

    1依赖 使用IDEA构建基于JPA的项目需要引用JPA.MYSQL依赖 2配置文件修改 2.1连接库 spring.datasource.url=jdbc:mysql://localhost:3306 ...

  10. lsscsi命令详解

    基础命令学习目录首页 lsscsi包默认是不安装的.lsscsi包安装完之后,lsscsi命令就可以使用了.lsscsi命令(lsscsi -t -L)能很方便的看出哪些是固态硬盘(SSD),哪些是S ...