例如mybatis,在yml中添加配置信息,就可以使用mybatis了。问题是yml中配置是如何来的?
可以通过以下方式找到:
我们都知道springboot的开发者,把一些我们常用的开发环境进行了默认的配置,拿这些配置文件在哪里呢,
我们在开发springboot的项目时是依赖maven进行开发的。导入的依赖包都在 Maven Dependenceis 中
在我们导入的依赖都是jar包,默认的配置信息存在 包名字以 *-autoconfigure.jar 形式结尾的jar包中,springboot的默认配置文件
配置文件的名字为:/META-INF/spring-configuration-metadata.json。
注意点: *-autoconfigure.jar 这种包很有可能 会有多个,因为这些*-autoconfigure.jar 包 与你在 maven中的依赖 有关,
springboot 并不是把 所有的默认配置都写在 了同一个文件中,而是分布在springboot的各个依赖的节点中。springboot的每一个
依赖节点比如:<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
在这个依赖中 对应的 默认配置信息在maven Dependenceis中的包为:spring-boot-autoconfigure-XXX.jar,XXX 代表版本号,
根据你自己的引入的版本决定。
在比如mybatis与springboot的结合的依赖节点:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
在这个依赖中 对应的 默认配置信息在maven Dependenceis中的包为:mybatis-spring-boot-autoconfigure-1.3.1.jar
我们可以在这种包中找到: /META-INF/spring-configuration-metadata.json。文件 这里面存在默认默认的配置。
当然还有一个文件/META-INF/additional-spring-configuration-metadata.json,这个文件中也是默认配置,是
/META-INF/spring-configuration-metadata.json 文件中的补充配置信息
我发现一下两个依赖节点对应的配置jar包是同一个 默认配置jar包:/META-INF/spring-configuration-metadata.json。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
当我去掉spring-boot-starter-data-jpa的依赖的时候,默认配置中的的 5776行处有配置
{
"name": "spring.jpa.hibernate.naming.physical-strategy",
"providers": [{
"name": "class-reference",
"parameters": {"target": "org.hibernate.boot.model.naming.PhysicalNamingStrategy"}
}]
},
但是org.hibernate.boot.model.naming.PhysicalNamingStrategy 类在我们的开发环境中不存在,
因为我们去除了依赖spring-boot-starter-data-jpa,自然就去除了关于jpa 的jar包,经过测试springboot 依然还是很好的额启动,
并不会报class not found Exception,这说明springboot在加载默认配置文件的时候会自动的过滤掉没有对应jar包的配置文件
---------------------
原文:https://blog.csdn.net/qq_29499107/article/details/82016753